#if defined CONFIG_RESOLVER&& defined CONFIG_RESOLVER_MPEGTS
#define MPEGTS_C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/mstime.h>
#include <libmscommon/mstypes.h>
#include <libmscommon/mscrc.h>
#include <libmscommon/msstring.h>
#include <libmscommon/msnetwork.h>
#include <libmscommon/mssignal.h>
#include <libmscommon/msbitrate.h>
#include <libmslog/mslog.h>
#include "mssi.h"
#include "msbitStream.h"
#include "mpegts.h"
#include "pes.h"
#include "pat.h"
#include "pmt.h"
#include "sdt.h"
#include "program.h"

#define FLAG "MPEGTS"

#define DBG_COMMON		ms_false
#define DBG_SYNC		ms_false
#define DBG_FIND_INFO	ms_false
#define DBG_BTC_PCR_PTSDTS 		ms_false
#define DBG_PAT			ms_true
#define DBG_SDT			ms_true
#define DBG_PMT			ms_true
/*PID映射调试*/
#define DBG_MAP_PAT		ms_false
#define DBG_MAP_SDT		ms_false
#define DBG_MAP_PMT		ms_false
#define DBG_MAP_ES		ms_false
#define DBG_MAP_PCR		ms_false
/*CC调试*/
#define DBG_CC_PID	0xFF
/*函数接口调用会被重新配置的参数*/
static ms_byte DBG_TSHEAD_MODE=0;	//0-不打印；1-详细信息；2-CC信息；

static ms_bool DBG_PCR	=ms_false;
static ms_bool DBG_CC	=ms_false;
static ms_bool DBG_AV	=ms_false;
static ms_bool DBG_PES	=ms_false;
static ms_bool DBG_PES_PTSDTS=ms_false;

#define lost_pat		(1<<0)	//1
#define lost_pmt		(1<<1)	//2
#define lost_pcr		(1<<2)	//4
#define lost_audio		(1<<3)	//8
#define lost_video		(1<<4)	//16
#define lost_subtitle	(1<<5)	//32
#define lost_esOther	(1<<6)	//64

typedef enum MPEGTSPKTType{
	pktType_unknow,
	pktType_pat,	//Program Allocation Table，节目关联表
	pktType_cat,	//Conditional Access Table，条件接收表
	pktType_tsdt,	//Transport Stream Descriptor Table，传输流描述符表
	pktType_pmt,	//Program Map Table，节目映射表
	pktType_nit,	//Network Information Table，网络信息表
	pktType_bat,	//Bouquet Association Table，业务群关联表
	pktType_sdt,	//Service Definition Table，服务定义表
	pktType_eit,	//Event Information Table，事件信息表
	pktType_tdt,	//Time and Date Table，时间日期表
	pktType_tot,	//Time Offset Table，时间偏移表
	pktType_rst,	//Running Status Table，运行状态表
	pktType_st,		//Stuffing Table，填充表
	pktType_dit,	//Discontinuity Information Table，不连续性信息表
	pktType_sit,	//Selection Information Table，选择信息表
	pktType_null,	
	pktType_pcr,
	pktType_es,
	pktType_max	
}MPEGTSPKTType;
typedef struct PMTInfo{
	ms_byte tag[256];
	ms_bool flag_findInfo;
	ms_u16 index;
//PMT表信息
	PMTTABLEInfo tableInfo;
//从PAT表获取的PMT的PID和program_num，注意：program_num为0时network_pid	
	ms_u16 program_num;
	ms_u16 pid;
//计算当前PCR数值
	ms_s64 pcr;
//计算PCR发送间隔
	MPEGTSPeriod periodPcr;
	MPEGTSPeriod periodPmt;
//指向自己的SDT	
	SDTInfo *sdt_info;
}PMTInfo;
typedef struct PMTINFOList{
	ms_u32 num;
	PMTInfo *pmtInfo[MAX_PROGRAM];
}PMTINFOList;
typedef struct MPEGTSContxt{
	ms_u32 mode;
	ms_u08 pktSize;
//TS头部信息
	TSHEADInfo headInfo;
//PAT信息
	PATTABLEInfo paInfo;
//PMT info
	PMTInfo pmtInfo[MAX_PROGRAM];	
//SDT info
	SDTTABLEInfo sdtInfo;
//主要用于打印
	char tag[256];
//MPEGTS的获取标记信息
	ms_bool flag_reparse;
	ms_bool flag_findAllInfo;
	ms_bool flag_findPatInfo;
	ms_bool flag_update;	//节目信息更新
	ms_u16 num_findPmtInfo; //已获取PMT表数量
//码率控制相关结构
	ESInfo *pEsInfo_btcPtsDts; //指向控制码率的ES流:使用视频ES，是因为有些音频ES无法控制
	RATEEMUContxt sbrt_pcr;
	RATEEMUContxt sbrt_pts;
	RATEEMUContxt sbrt_dts;
//PAT和PMT原始数据	
	MPEGTSPeriod periodPat;
	MPEGTSPeriod periodSdt;
//PID映射参数
	ms_bool flag_map_program_num;
	ms_bool flag_map_pmt_pid;
	ms_bool flag_map_pcr_pid;
	ms_bool flag_map_es;
	ms_bool flag_map_name;
	MAPLISTContxt *mapList_ctt;
	PIDLISTContxt *pidList_ctt;
	STREAMInfo *stream_info;
/*重构表信息*/	
	SDTPARAMContxt sdt_ctt;
	PATPARAMContxt pat_ctt;
	PMTPARAMContxt pmt_ctt[MAX_PROGRAM];
	ms_u08 sdt_cc;
	ms_u08 pat_cc;	
	ms_s64 Etimep_pat_pmt;
	ms_s64 Etimep_sdt;	
}MPEGTSContxt;

static char *str_serviceType[servicetype_max]={ms_null};
static char *str_streamType[streamtype_max]={ms_null};
static char *str_avType[avtype_max]={ms_null};

#define UNKNOW_TYPE	"unknow"

#define FILE_LOG_TSMONITOR			"/var/log/mscore/msmsg_tsMonitor.txt"
#define ENABLE_LOGFILE 	ms_true

static ms_void mpegts_innerapi_msg(STREAMInfo *stream_info,ms_string armInfo)
{
	if(ENABLE_LOGFILE){
		ms_logfile(FILE_LOG_TSMONITOR, "告警信息:[%s]%s",stream_info->tag,armInfo);
	}
	ms_waring("告警信息:[%s]%s",stream_info->tag,armInfo);
}
static ms_void mpegts_innerapi_getPidErrInfo(ERRLEVEL1Contxt *perr1_ctt,ms_byte *pidErr_info)
{
	if(!perr1_ctt->pidErr){
		ms_sprintfs(pidErr_info, "%u",perr1_ctt->pidErr);
	}else{
		ms_byte pidErr_info_tmp[1024]={0};
		if(perr1_ctt->pidErr&lost_pat){
			if(ms_buflen(pidErr_info_tmp)){ms_strcat(pidErr_info_tmp, "+");}	
			ms_strcat(pidErr_info_tmp, "PAT");
		}
		if(perr1_ctt->pidErr&lost_pmt){
			if(ms_buflen(pidErr_info_tmp)){ms_strcat(pidErr_info_tmp, "+");}		
			ms_strcat(pidErr_info_tmp, "PMT");
		}
		if(perr1_ctt->pidErr&lost_pcr){
			if(ms_buflen(pidErr_info_tmp)){ms_strcat(pidErr_info_tmp, "+");}		
			ms_strcat(pidErr_info_tmp, "PCR");
		}
		if(perr1_ctt->pidErr&lost_audio){
			if(ms_buflen(pidErr_info_tmp)){ms_strcat(pidErr_info_tmp, "+");}		
			ms_strcat(pidErr_info_tmp, "audio");
		}
		if(perr1_ctt->pidErr&lost_video){
			if(ms_buflen(pidErr_info_tmp)){ms_strcat(pidErr_info_tmp, "+");}		
			ms_strcat(pidErr_info_tmp, "video");
		}
		if(perr1_ctt->pidErr&lost_subtitle){
			if(ms_buflen(pidErr_info_tmp)){ms_strcat(pidErr_info_tmp, "+");}		
			ms_strcat(pidErr_info_tmp, "subtitle");
		}
		if(perr1_ctt->pidErr&lost_esOther){
			if(ms_buflen(pidErr_info_tmp)){ms_strcat(pidErr_info_tmp, "+");}		
			ms_strcat(pidErr_info_tmp, "esOther");
		}
		if(ms_buflen(pidErr_info_tmp)){
			ms_sprintfs(pidErr_info, "%u-%s",perr1_ctt->pidErr,pidErr_info_tmp);
		}else{
			ms_sprintfs(pidErr_info, "%u",perr1_ctt->pidErr);
		}
	}
}
static ms_void mpegts_innerapi_showPidInfo(STREAMInfo *stream_info)
{
/*PID统计*/	
	if(stream_info->pidList_ctt.streamrate_info.average_bitrate_info.runtime>0){
		ms_info3("-------------------------------------------------------");
		ms_info3("Active PIDs:");
		ms_u64 runtime=(ms_u64)stream_info->pidList_ctt.streamrate_info.average_bitrate_info.runtime/ms_usseconds(1);
		ms_byte runtime_buf[256]={0};
		mstime_api_sec2str2((ms_u32 )runtime, runtime_buf);
		ms_info3("  Running Time: %s s", runtime_buf);
		ms_info3("  Mux Bitrate: %d kbps", stream_info->pidList_ctt.streamrate_info.average_bitrate_info.bitrate);
		ms_info3("  Last Sec: %d kbps",stream_info->pidList_ctt.streamrate_info.realtime_bitrate_info.bitrate);
		ms_s32 iPbr_t=0;
		for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
			PIDContxt *pbr_ctt=&stream_info->pidList_ctt.pid_ctt[iPbr];
			if(!pbr_ctt->flag_use){
				break;
			}
			mpegts_api_getPidInfo(&stream_info->pidList_ctt,pbr_ctt,stream_info);
		}
		for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
			PIDContxt *pbr_ctt=&stream_info->pidList_ctt.pid_ctt[iPbr];
			if(!pbr_ctt->flag_use){
				break;
			}
			if(SI_PID_UNVALID==pbr_ctt->program_num){
				ms_string info= ms_buflen(pbr_ctt->info) ? pbr_ctt->info: "unkonw";
				ms_info3("  %03u 0x%04x(%04u), %s, %s, %d kbps", (++iPbr_t),pbr_ctt->pid,pbr_ctt->pid,pbr_ctt->percentage,
					info,pbr_ctt->streamrate_info.realtime_bitrate_info.bitrate);
			}
		}
		for(ms_s32 index=0;index<stream_info->totalnum_program;index++){
			PROGRAMInfo *program_info=&stream_info->program_info[index];
			for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
				PIDContxt *pbr_ctt=&stream_info->pidList_ctt.pid_ctt[iPbr];
				if(!pbr_ctt->flag_use){
					break;
				}
				if(program_info->program_num==pbr_ctt->program_num){
					ms_string info= ms_buflen(pbr_ctt->info) ? pbr_ctt->info: "unkonw";
					ms_info3("  %03u 0x%04x(%04u), %s, %s, %d kbps", (++iPbr_t),pbr_ctt->pid,pbr_ctt->pid,pbr_ctt->percentage,
						info,pbr_ctt->streamrate_info.realtime_bitrate_info.bitrate);
				}
			}		
		}
		for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
			PIDContxt *pbr_ctt=&stream_info->pidList_ctt.pid_ctt[iPbr];
			if(!pbr_ctt->flag_use){
				break;
			}
			if(0==pbr_ctt->program_num){
				ms_string info= ms_buflen(pbr_ctt->info) ? pbr_ctt->info: "unkonw";
				ms_info3("  %03u 0x%04x(%04u), %s, %s, %d kbps", (++iPbr_t),pbr_ctt->pid,pbr_ctt->pid,pbr_ctt->percentage,
					info,pbr_ctt->streamrate_info.realtime_bitrate_info.bitrate);
			}
		}
	}	
}
static ms_void mpegts_innerapi_showPidInfo2(STREAMInfo *stream_info)
{
/*PID统计*/	
	if(stream_info->pidList_ctt.streamrate_info.average_bitrate_info.runtime>0){
		ms_info3("-------------------------------------------------------");
		ms_info3("Active PIDs:");
		ms_u64 runtime=(ms_u64)stream_info->pidList_ctt.streamrate_info.average_bitrate_info.runtime/ms_usseconds(1);
		ms_byte runtime_buf[256]={0};
		mstime_api_sec2str2((ms_u32 )runtime, runtime_buf);
		ms_info3("  Running Time: %s s", runtime_buf);
		ms_info3("  Mux Bitrate: %d kbps", stream_info->pidList_ctt.streamrate_info.average_bitrate_info.bitrate);
		ms_info3("  Last Sec: %d kbps",stream_info->pidList_ctt.streamrate_info.realtime_bitrate_info.bitrate);
		ms_s32 iPbr_t=0;
		for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
			PIDContxt *pbr_ctt=&stream_info->pidList_ctt.pid_ctt[iPbr];
			if(!pbr_ctt->flag_use){
				break;
			}
			mpegts_api_getPidInfo(&stream_info->pidList_ctt,pbr_ctt,stream_info);
		}
		for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
			PIDContxt *pbr_ctt=&stream_info->pidList_ctt.pid_ctt[iPbr];
			if(!pbr_ctt->flag_use){
				break;
			}
	
			ms_string info= ms_buflen(pbr_ctt->info) ? pbr_ctt->info: "unkonw";
			ms_info3("  %03u 0x%04x(%04u), %s, %s, %d kbps", (++iPbr_t),pbr_ctt->pid,pbr_ctt->pid,pbr_ctt->percentage,
				info,pbr_ctt->streamrate_info.realtime_bitrate_info.bitrate);
		}
	}	
}
static ms_void mpegts_innerapi_showInfo(STREAMInfo *stream_info)
{
	ms_byte patPeriod[128]={0};
	ms_byte sdtPeriod[128]={0};
	mpegts_api_getPidErr(stream_info);
	mstime_api_us2str2(stream_info->period_pat.period,patPeriod);
	mstime_api_us2str2(stream_info->period_sdt.period,sdtPeriod);
	ms_info3("*************************************************************************************************");
	if(mpegts_api_isMpeg(stream_info->format,stream_info->program_info[0].pmt_pid)){
/*基本信息*/	
		ms_info3("Info: %s, pktSize(%d), progress(%d)",stream_info->format,stream_info->pktSize,stream_info->progress);
	}
	if(mpegts_api_isMpeg(stream_info->format,stream_info->program_info[0].pmt_pid)){
/*节目整体信息*/	
		if(stream_info->totalnum_program_real>stream_info->totalnum_program){
			ms_info3("ProgramInfo: TotalNum(%d|%d), stream[total:v:a]%d,%d,%d",
				stream_info->totalnum_program,stream_info->totalnum_program_real,stream_info->nb_streams,
				stream_info->nb_video,stream_info->nb_audio);
		}else{
			ms_info3("ProgramInfo: TotalNum(%d), stream[total:v:a]%d,%d,%d",
				stream_info->totalnum_program_real,stream_info->nb_streams,
				stream_info->nb_video,stream_info->nb_audio);
		}	
/*详细信息*/		
		ms_info3("PAT[0x%04x]: period(%s), cc(Err)%d",PAT_PID,patPeriod,stream_info->ctuInfo_pat.numErr);
		ms_info3("SDT[0x%04x]: period(%s), cc(Err)%d",SI_PID_SDT,sdtPeriod,stream_info->ctuInfo_sdt.numErr);
		for(ms_s32 index=0;index<stream_info->totalnum_program;index++){
			PROGRAMInfo *program_info=&stream_info->program_info[index];
			ms_info3("-------------------------------------------------------");
			ms_byte pmtPeriod[128]={0};
			mstime_api_us2str2(program_info->period_pmt.period,pmtPeriod);		
			ms_info3("PMT[0x%04x-%04u]-%04u period(%s), cc(Err)%d",program_info->pmt_pid,program_info->pmt_pid,program_info->program_num,pmtPeriod,program_info->ctuInfo_pmt.numErr);
			if(program_info->flag_hasSdt){
				ms_info3("  service_name: %s",program_info->service_name);
				ms_info3("  service_type: %s",program_info->service_type);
				ms_info3("  service_provider_name: %s",program_info->service_provider_name);
			}
			ms_s32 indexEs=0;
			for(indexEs=0;indexEs<program_info->num_es;indexEs++){
				ESInfo *esInfo=&program_info->esInfo[indexEs];
				ms_info3("    ES%d[0x%04x-%04u]#%d:%d: %s, %s, esinfo_len(%u), cc(Err)%u, numPcr_noInPmt(%u), numNonmomo(pts,dts)%u,%u",
					indexEs,esInfo->pid,esInfo->pid,program_info->num_es,indexEs,
					mpegts_api_getAVType(esInfo->avType),esInfo->strStreamType,esInfo->len_esInfo,
					esInfo->ctuInfo.numErr,
					esInfo->numPcr_noInPmt,esInfo->numNonmomo[0],esInfo->numNonmomo[1]);
			}
			ms_byte pcrPeriod[128]={0};
			mstime_api_us2str2(program_info->period_pcr.period,pcrPeriod);
			if(program_info->sharePid){
				ms_info3("    PCR[0x%04x-%04u]period(%s)",program_info->pcr_pid,program_info->pcr_pid,pcrPeriod);
			}else{
				ms_info3("    PCR[0x%04x-%04u]period(%s), cc(Err)%u",program_info->pcr_pid,program_info->pcr_pid,pcrPeriod,
					program_info->ctuInfo_pcr.numErr);
			}
		}
	}
/*PID统计*/	
	mpegts_innerapi_showPidInfo(stream_info);
/*错误统计*/		
	ms_info3("-------------------------------------------------------");
//第一级错误（致命错误）
	ERRLEVEL1Contxt *perr1_ctt=&stream_info->monitorInfo.errLevel1_ctt;
	ms_byte pidErr_info[1024]={0};
	mpegts_innerapi_getPidErrInfo(perr1_ctt, pidErr_info);
	ms_info3("ErrInfo_level1: syncLost(%u), syncErr(%u), patErr(%u), ccErr(%u),ccErr_av(%u), pmtErr(%u),""pidErr(%s)",
		perr1_ctt->syncLost,perr1_ctt->syncErr,perr1_ctt->patErr,perr1_ctt->ccErr,perr1_ctt->ccErr_av,perr1_ctt->pmtErr,pidErr_info);
//第二级错误（服务质量问题）
	ERRLEVEL2Contxt *perr2_ctt=&stream_info->monitorInfo.errLevel2_ctt;
	ms_info3("ErrInfo_level2: TEI(%u), crcErr(%u), pcrIntervalErr(%u), pcrDIErr(%u), ptsErr(%u)",
		perr2_ctt->TEI,perr2_ctt->crcErr,perr2_ctt->pcrIntervalErr,perr2_ctt->pcrDIErr,perr2_ctt->ptsErr);
//三级优先级错误（轻微问题）
	ERRLEVEL3Contxt *perr3_ctt=&stream_info->monitorInfo.errLevel3_ctt;	
	ms_info3("ErrInfo_level3: infoChange(%u), sdtErr(%u)",perr3_ctt->infoChange,perr3_ctt->sdtErr);	
}
static ms_void mpegts_innerapi_showPesStreamId(ESInfo *pEsInfo,ms_u08 pes_streamid)
{
	switch (pes_streamid){
		case PES_STREAM_ID_PSM:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-psm",pEsInfo->pid,pes_streamid);
			break;
		case PES_STREAM_ID_PRIVATE_1:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-private1",pEsInfo->pid,pes_streamid);
			break;
		case PES_STREAM_ID_PADDING:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-padding",pEsInfo->pid,pes_streamid);
			break;
		case PES_STREAM_ID_PRIVATE_2:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-private2",pEsInfo->pid,pes_streamid);
			break;
		case PES_STREAM_ID_ECM:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-ecm",pEsInfo->pid,pes_streamid);
			break;
		case PES_STREAM_ID_EMM:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-emm",pEsInfo->pid,pes_streamid);
			break;	
		case PES_STREAM_ID_DSMCC:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-dsmcc",pEsInfo->pid,pes_streamid);
			break;
		case PES_STREAM_ID_MHEG:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-mheg",pEsInfo->pid,pes_streamid);
			break;
		case PES_STREAM_ID_H222_1_E:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-h222_1_e",pEsInfo->pid,pes_streamid);
			break;	
		case PES_STREAM_ID_PSD:
			ms_enDbg(DBG_PES, "[%#x]流ID:%#x-psd",pEsInfo->pid,pes_streamid);
			break;	
		default:
			if(ms_range(pes_streamid, PES_STREAM_ID_AUDIO_MPEG, (PES_STREAM_ID_VIDEO_MPEG-1))){
				ms_enDbg(DBG_PES, "[%#x]流ID:%#x-audio",pEsInfo->pid,pes_streamid);
			}else if(ms_range(pes_streamid, PES_STREAM_ID_VIDEO_MPEG, (PES_STREAM_ID_ECM-1))){
				ms_enDbg(DBG_PES, "[%#x]流ID:%#x-video",pEsInfo->pid,pes_streamid);
			}else{
				ms_enDbg(DBG_PES, "[%#x]流ID:%#x-错误",pEsInfo->pid,pes_streamid);
			}
			break;	
	}
}
static ms_void mpegts_innerapi_getTag(MPEGTSContxt *rlmpegts,ms_string exStr,ms_byte *outBuf)
{
	if(!ms_buflen(outBuf)){
		ms_sprintfs(outBuf,"%s-0x%04x(%s)",rlmpegts->tag,rlmpegts->headInfo.pid,exStr);
	}
}
static ms_void mpegts_innerapi_syncLost(MPEGTSContxt *rlmpegts,ms_s32 len,ms_s32 pkt_index)
{
	ms_string info=ms_null;
	if(len%rlmpegts->pktSize){
		info="Sync losses";
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.syncLost+=1;
	}else{
		info="Sync byte error";
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.syncErr+=1;
	}
	ms_enWar(DBG_SYNC,"[%s]%s(len:%d,index:%d)",rlmpegts->tag,info,len,pkt_index);
}
static ms_bool mpegts_innerapi_isGetAllInfo(MPEGTSContxt *rlmpegts)
{
/*计算获取了多少套节目信息*/	
	ms_s32 num_pmtInfo=0;
	for(ms_s32 program_index=0;program_index<rlmpegts->paInfo.totalnum_program;program_index++){
		if(ms_true==rlmpegts->pmtInfo[program_index].flag_findInfo){
			num_pmtInfo+=1;
		}
	}
	rlmpegts->num_findPmtInfo=num_pmtInfo;
	
/*更新解析进度和状态*/	
//	ms_debug("num_findPmtInfo,totalnum_program:%d,%d(flag_findAllInfo:%d)",rlmpegts->num_findPmtInfo,rlmpegts->paInfo.totalnum_program,rlmpegts->flag_findAllInfo);
	if(rlmpegts->stream_info->progress<100&&rlmpegts->paInfo.totalnum_program>0){
		rlmpegts->stream_info->progress=rlmpegts->num_findPmtInfo*100/rlmpegts->paInfo.totalnum_program;
	}

/*判断信息是否获取完成：PMT表信息数应该等于节目总数*/	
	//ms_debug("totalnum_program:%d,num_pmtInfo:%d", rlmpegts->patTableInfo.totalnum_program,num_pmtInfo);
	if(0!=rlmpegts->paInfo.totalnum_program&&num_pmtInfo==rlmpegts->paInfo.totalnum_program){
		ms_info("Find all info,totalnum_program:%d,num_pmtInfo:%d", rlmpegts->paInfo.totalnum_program,num_pmtInfo);
		//mpegts_innerapi_showInfo(rlmpegts,ms_false);
		return ms_true;
	}else{
		ms_enDbg(DBG_FIND_INFO,"program_num,num_pmtInfo:%d,%d",rlmpegts->paInfo.totalnum_program,num_pmtInfo);
		return ms_false;
	}

}
static ms_void mpegts_innerapi_pidStatistics(MPEGTSContxt *rlmpegts)
{	
/*数据太大，重置数据*/
	if(++rlmpegts->stream_info->pidList_ctt.totalPid>((ms_f32-5)/1000)){
		rlmpegts->stream_info->pidList_ctt.totalPid=1;
		for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
			PIDContxt *pbr_ctt=&rlmpegts->stream_info->pidList_ctt.pid_ctt[iPbr];
			if(pbr_ctt->flag_use){
				pbr_ctt->num=0;
			}
		}
	}
	for(ms_s32 iPbr=0;iPbr<MAX_STREAM;iPbr++){
		PIDContxt *pbr_ctt=&rlmpegts->stream_info->pidList_ctt.pid_ctt[iPbr];
		//ms_info("%d--%#x-------%d,%#x", iPbr,rlmpegts->tsHeadInfo.pid,pbr_ctt->flag_use,pbr_ctt->pid)
		if(pbr_ctt->flag_use){
			if(pbr_ctt->pid==rlmpegts->headInfo.pid){
				pbr_ctt->num+=1;
				if(!mpegts_innerapi_filterNullPkt(rlmpegts)){
					msbitrate_api_clac(&rlmpegts->stream_info->pidList_ctt.streamrate_info,rlmpegts->pktSize, flags_bitrate_caverage_and_realtime);
				}
				msbitrate_api_clac(&pbr_ctt->streamrate_info,rlmpegts->pktSize, flags_bitrate_realtime);
				break;
			}
		}else{
		/*新的PID*/
			pbr_ctt->flag_use=ms_true;
			pbr_ctt->pid=rlmpegts->headInfo.pid;
			pbr_ctt->num+=1;
			if(!mpegts_innerapi_filterNullPkt(rlmpegts)){
				msbitrate_api_clac(&rlmpegts->stream_info->pidList_ctt.streamrate_info,rlmpegts->pktSize, flags_bitrate_caverage_and_realtime);
			}
			msbitrate_api_clac(&pbr_ctt->streamrate_info,rlmpegts->pktSize, flags_bitrate_realtime);
			break;
		}
	}
}
/*根据表PID和调整字段情况进行二次判断*/
static ms_s32 mpegts_innerapi_isTargstTable(ms_string tableName,MPEGTSContxt *rlmpegts,char * pes_buf,
	ms_u08 adaptation_field_len,ms_u16 tablePid,ms_bool flag_debug)
{
	ms_enDbg(flag_debug,"[%s]-------adaptation_field_len: %d byte----------",tableName,adaptation_field_len);
	if(ms_noLess(adaptation_field_len, (TS_SIZE_PES-1))){
		return -1;
	}
	ms_u08 payload_offset=adaptation_field_len;
	ms_u08 table_offset=0;
	ms_enDbg(flag_debug,"[%s]Find tid(%#x,%#x,%#x)",tableName,pes_buf[payload_offset+0]&0XFF,
		pes_buf[payload_offset+1]&0XFF,pes_buf[payload_offset+2]&0XFF);
	//由于调整字段的影响，可能表头出现位置不同
	if( tablePid==(pes_buf[payload_offset+0]&0XFF) &&0x80==(pes_buf[payload_offset+1]&0x80)	){
		payload_offset+=0;
		table_offset+=0;
	}else if(tablePid==(pes_buf[payload_offset+1]&0XFF) &&0x80==(pes_buf[payload_offset+2]&0x80)	){
		payload_offset+=1;
		table_offset+=1;
	}else{
		ms_waring("Maybe It isnot a %s pkt(%#x,%#x,%#x)",tableName,pes_buf[payload_offset+0]&0XFF,
			pes_buf[payload_offset+1]&0XFF,pes_buf[payload_offset+2]&0XFF);
		//ms_enBufHex(flag_debug,tableName, pes_buf,TS_SIZE_PES);
		ms_bufHex(tableName, pes_buf, TS_SIZE_PES);
		mssi_api_headerPrint(&rlmpegts->headInfo,ms_true);
		return -1;
	}
	return table_offset;
}
static ms_void mpegts_innerapi_getPeriod(MPEGTSPeriod *pPeriod)
{
	pPeriod->sys_cur=mstime_api_upUs();
	if(0!=pPeriod->sys_pre){
		pPeriod->period=pPeriod->sys_cur-pPeriod->sys_pre;
	}
	pPeriod->sys_pre=pPeriod->sys_cur;
}
/*通过PCR的PID来找到pmt_info(兼容多个节目共用同一个PCR的情况)*/	
static ms_void mpegts_innerapi_findPmt_byPcrPid(MPEGTSContxt *rlmpegts,ms_u16 pcr_pid,PMTINFOList *pmtInfo_list)
{
	ms_memset(pmtInfo_list, 0, ms_bufsize(PMTINFOList));
	for(ms_u16 index=0;index<rlmpegts->paInfo.totalnum_program;index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[index];
		if(mssi_api_isValidPid(pcr_pid)&&pmt_info->tableInfo.pid_pcr==pcr_pid){
			pmtInfo_list->pmtInfo[pmtInfo_list->num]=pmt_info;
			pmtInfo_list->num+=1;
		}	
	}
}
static ms_u32 mpegts_innerapi_getSharPidNum_byPcrPid(MPEGTSContxt *rlmpegts,ms_u16 pcr_pid)
{
	ms_u32 num=0;
	for(ms_u16 index=0;index<rlmpegts->paInfo.totalnum_program;index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[index];
		if(mssi_api_isValidPid(pcr_pid)&&pmt_info->tableInfo.pid_pcr==pcr_pid){
			num+=1;
		}	
	}
	return num;
}

/*通过ES的PID来找到pmt_info*/
static PMTInfo * mpegts_innerapi_findPmt_byEsPid(MPEGTSContxt *rlmpegts,ms_u16 es_pid)
{
	for(ms_s32 program_index=0;program_index<rlmpegts->paInfo.totalnum_program;program_index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[program_index];
		for(ms_s32 es_index=0;es_index<pmt_info->tableInfo.num_es;es_index++){			
			if(mssi_api_isValidPid(es_pid)&&es_pid==pmt_info->tableInfo.esInfo[es_index].pid){
				return pmt_info;
			}
		}
	}
	return ms_null;
}
/*通过PMT的PID和program_num来找到pmt_info*/
static PMTInfo * mpegts_innerapi_findPmt(MPEGTSContxt *rlmpegts,ms_u16 program_num)
{
	for(ms_s32 program_index=0;program_index<rlmpegts->paInfo.totalnum_program;program_index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[program_index];				
		if(mssi_api_isValidPid(pmt_info->pid)&&pmt_info->pid==rlmpegts->headInfo.pid){
			if(pmt_info->program_num>0){//program_num为0时network_pid，遇见过多个节目共用一个PMT的	
				if(pmt_info->program_num==program_num){
					return pmt_info;
				}
			}else{
				return pmt_info;
			}
		}	
	}
	return ms_null;
}
static ms_bool mpegts_innerapi_isPmt_byPid(MPEGTSContxt *rlmpegts,ms_u16 pmt_pid)
{
	for(ms_s32 program_index=0;program_index<rlmpegts->paInfo.totalnum_program;program_index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[program_index];
		if(mssi_api_isValidPid(pmt_pid)&&pmt_info->pid==pmt_pid){
			return ms_true;
		}	
	}
	return ms_false;
}

static ESInfo * mpegts_innerapi_findEs(MPEGTSContxt *rlmpegts)
{
	for(ms_s32 program_index=0;program_index<rlmpegts->paInfo.totalnum_program;program_index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[program_index];
		for(ms_s32 es_index=0;es_index<pmt_info->tableInfo.num_es;es_index++){
			ESInfo *pEsInfo=&pmt_info->tableInfo.esInfo[es_index];
/*通过ES的PID来找到pEsInfo*/					
			if(pEsInfo->pid==rlmpegts->headInfo.pid){
				return pEsInfo;
			}
		}
	}
	return ms_null;
}

static ESInfo * mpegts_innerapi_getBtcEs(MPEGTSContxt *rlmpegts)
{
	ESInfo *pEsInfo_tmp=ms_null;
	for(ms_s32 program_index=0;program_index<rlmpegts->paInfo.totalnum_program;program_index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[program_index];
		for(ms_s32 es_index=0;es_index<pmt_info->tableInfo.num_es;es_index++){
			if(ms_null==pEsInfo_tmp){
				pEsInfo_tmp=&pmt_info->tableInfo.esInfo[es_index];
			}
			ESInfo *pEsInfo=&pmt_info->tableInfo.esInfo[es_index];
			if(avtype_video==pEsInfo->avType){
				ms_enDbg(DBG_PES, "使用ES[%d]来控制码率", pEsInfo->pid);
				return pEsInfo;
			}
		}
	}
	ms_enDbg(DBG_PES, "使用ES[%d]来控制码率", pEsInfo_tmp->pid);
	return pEsInfo_tmp;
}
ms_bool mpegts_innerapi_rateEmuDone(char * tag,char * exStr,RATEEMUContxt * sbrt,ms_s64 timeStream_cur,
	ms_s64 timeSys_cur,ms_bool flag_dbg)
{
	ms_s64 ctime_sys=timeSys_cur-sbrt->timeSys_pre; 
	ms_s64 ctime_stream=timeStream_cur-sbrt->timeStream_pre ;//us
	ms_enDbg(DBG_BTC_PCR_PTSDTS,"[%s:%s]当前系统时间:%s,上次系统时间:%s,差值:%s", tag,exStr,ms_num2strS64(timeSys_cur),
		ms_num2strS64(sbrt->timeSys_pre),ms_num2strS64(ctime_sys)); 
	ms_enDbg(DBG_BTC_PCR_PTSDTS,"[%s:%s]当前流时间:%s,上次流时间:%s,差值:%s", tag,exStr,ms_num2strS64(timeStream_cur),
		ms_num2strS64(sbrt->timeStream_pre),ms_num2strS64(ctime_stream)); 
	ms_s64 cctime_us=ctime_stream-ctime_sys;
/*超出有效范围，无法进行运算，复位吧(无穷小,(-BTC_PST_UNVALID)]，[BTC_PST_UNVALID,无穷大)*/
	if(ms_outRange2(cctime_us, (-BTC_PST_UNVALID), BTC_PST_UNVALID)){
		sbrt->num_failed+=1;
		ms_waring("[%s:%s]cctime_us(%s) out of range (%s-%s),当前系统时间:%s,上次系统时间:%s,差值:%s,当前流时间:%s,上次流时间:%s,差值:%s", 
			tag,exStr,ms_num2strS64(cctime_us),ms_num2strS64((-BTC_PST_UNVALID)),ms_num2strS64(BTC_PST_UNVALID),
			ms_num2strS64(timeSys_cur),ms_num2strS64(sbrt->timeSys_pre),ms_num2strS64(ctime_sys),
			ms_num2strS64(timeStream_cur),ms_num2strS64(sbrt->timeStream_pre),ms_num2strS64(ctime_stream));  
		return ms_false;
	}

/*发送太快，需要等等(BTC_PST_SYSUSE,BTC_PST_UNVALID)*/
	ms_s64 ccctime_us=cctime_us-BTC_PST_SYSUSE;
	if(ccctime_us>0){
		ms_enDbg(DBG_BTC_PCR_PTSDTS,"[%s:%s]ms_usleep:%s", tag,exStr,ms_num2strS64(ccctime_us)); 
		ms_usleep(ccctime_us );
/*发送太慢，尽快发出去((-BTC_PST_UNVALID),BTC_PST_SYSUSE]*/	
	}else{
		ms_enWar(flag_dbg,"[%s:%s]must send fast :cctime_us:%s,当前系统时间:%s,上次系统时间:%s,差值:%s,当前流时间:%s,上次流时间:%s,差值:%s", 
			tag,exStr,ms_num2strS64(cctime_us),
			ms_num2strS64(timeSys_cur),ms_num2strS64(sbrt->timeSys_pre),ms_num2strS64(ctime_sys),
			ms_num2strS64(timeStream_cur),ms_num2strS64(sbrt->timeStream_pre),ms_num2strS64(ctime_stream));  
	}
	sbrt->num_failed=0;
	return ms_true;
}
ms_bool mpegts_innerapi_isRateEmuVaild(RATEEMUContxt * sbrt,ms_s64 timeStream_cur,ms_s64 timeSys_cur)
{
	return ((0!=sbrt->timeSys_pre)&&(0!=sbrt->timeStream_pre)
		&&(timeSys_cur>sbrt->timeSys_pre)&&(timeStream_cur>sbrt->timeStream_pre)&&(sbrt->num_failed<2)) ? ms_true : ms_false;
}

static MAPContxt * mpegts_innerapi_findMap(MPEGTSContxt *rlmpegts,ms_u16 program_num,ms_bool flag_dbg)
{
	if(!rlmpegts->mapList_ctt){
		ms_enErr(flag_dbg,"[%s]mapList_ctt为空",rlmpegts->tag);
		return ms_null;
	}
	for(ms_s32 indexRemap=0;indexRemap<rlmpegts->mapList_ctt->num_map;indexRemap++){
		MAPContxt *pmap_ctt=&rlmpegts->mapList_ctt->map_ctt[indexRemap];
/*通过program_num来查找pmap_ctt*/
		ms_enInfo(flag_dbg,"[%s]%03d program_num:%u,%u",rlmpegts->tag,indexRemap,pmap_ctt->program_num_org,program_num);
		if(pmap_ctt->program_num_org==program_num){
			ms_enInfo(flag_dbg,"[%s]找到节目%u",rlmpegts->tag,program_num);
			return pmap_ctt;
		}
	}
	ms_enErr(flag_dbg,"[%s]没有找到节目%u",rlmpegts->tag,program_num);
	return ms_null;
}
static ENUMAVType mpegts_innerapi_getAVType_byStreamtype(ms_u08 streamtype)
{
	ENUMAVType avType=avtype_unknow;	
	switch(streamtype){
		case streamtype_video_mpeg1:
		case streamtype_video_mpeg2:
			avType=avtype_video;
			break;	
		case streamtype_audio_mpeg1:
		case streamtype_audio_mpeg2:
			avType=avtype_audio;
			break;
		case streamtype_private_sections:	
		case streamtype_pes_private_data:	
			avType=avtype_data;
			break;
		case streamtype_ns_mheg:
		case streamtype_ns_AnnexA_DSM_CC :
		case streamtype_ns_h2221:
		case streamtype_ns_typeA:
		case streamtype_ns_typeB:
		case streamtype_ns_typeC:
		case streamtype_ns_typeD:
		case streamtype_ns_auxiliary:
			avType=avtype_nb;
			break;
		case streamtype_audio_aac:	
			avType=avtype_audio;
			break;	
		case streamtype_video_mpeg4:	
			avType=avtype_video;
			break;
		case streamtype_audio_aac_latm:
			avType=avtype_audio;
			break;	
		case streamtype_ns_FlexMux_pes:
		case streamtype_ns_FlexMux_14496sec:
		case streamtype_ns_sync_download:
			avType=avtype_nb;
			break;
		case streamtype_metadata:
			avType=avtype_attachment;
			break;
		case streamtype_ns_metadata_sections:
		case streamtype_ns_metadata_138186DataCarousel:
		case streamtype_ns_metadata_138186ObjectCarousel:
		case streamtype_ns_metadata_138186sync_download:
		case streamtype_ns_mpeg2_ipmp:
			avType=avtype_nb;
			break;
		case streamtype_video_h264: 
		case streamtype_video_hevc: 
		case streamtype_video_cavs: 
			avType=avtype_video;
			break;
		case streamtype_ns_ipmp: 
			avType=avtype_nb;
			break;
		case streamtype_audio_ac3:	
		case streamtype_audio_dts:
		case streamtype_audio_truehd:
			avType=avtype_audio;
			break;
		case streamtype_ns_sit: 
			avType=avtype_nb;
			break;
		case streamtype_audio_eac3: 
			avType=avtype_audio;
			break;	
		case streamtype_subtitle_pgs: 
		case streamtype_subtitle_igs: 
		case streamtype_subtitle_text: 
			avType=avtype_subtitle;
			break;				
		case streamtype_video_vc1:	
		case streamtype_video_dirac:	
			avType=avtype_video;
			break;
		default:
			break;					
	}
	return avType;
}
static ms_void mpegts_innerapi_tableErr(MPEGTSContxt *rlmpegts,ms_u16 tablePid)
{
	if( SI_TID_PAT==tablePid){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
	}else if( SI_TID_SDT_ACT==tablePid){
		rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
	} 
}
static ms_void mpegts_innerapi_ccErr(MPEGTSContxt *rlmpegts,ms_u16 tablePid)
{
	if( SI_TID_PAT==tablePid){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
		rlmpegts->stream_info->monitorInfo.errLevel2_ctt.crcErr+=1;
	}else if( SI_TID_SDT_ACT==tablePid){
	/*SDT错误不会造成重大问题，因此不计入CRC错误*/
		rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
	} 
}
static ms_bool mpegts_innerapi_recvOK(SECTIONData *psec_data)
{
	ms_u16 len_payload1=psec_data->len_recv-psec_data->adaptation_field_len;
	ms_u16 len_payload2=psec_data->len_section;
	if(psec_data->len_section
		&&ms_noLess(psec_data->len_recv, psec_data->adaptation_field_len)
		&&ms_noLess(len_payload1, len_payload2)){
		return ms_true;
	}else{
		return ms_false;
	}
}
static ms_u16 mpegts_innerapi_getSectionData_one(MPEGTSContxt *rlmpegts,SECTIONData *psec_data,
	ms_u16 tablePid,ms_byte *bufOut,ms_bool flag_dbg,ms_string destype)
{
	if(!psec_data->flag_recvOk){
		return 0;
	}
	ms_u08 adaptation_field_len=psec_data->adaptation_field_len;
	ms_u16 len_section=psec_data->len_section;
	ms_byte *psection_data=psec_data->data;

/*计算CRC*/
	ms_u32 crc=ms_host_4b((&psection_data[len_section+adaptation_field_len]));
	ms_enDbg(flag_dbg,"[%s]crc:%#x=%u",destype,crc,crc);
	ms_u32 crc_calac=mscrc_api_CRC32(&psection_data[1+adaptation_field_len], (len_section-1));
	if(crc!=crc_calac){
		mpegts_innerapi_ccErr(rlmpegts,tablePid);
		ms_errRet(0,"[%s]crc(%#x)!=crc_calac(%#x),maybe pes_payload is error",destype,crc,crc_calac);
	}

/*去掉调整字段和头部*/
	ms_u08 offset=adaptation_field_len+LEN_SECTION_HEADER(psec_data->table_offset); 
//ms_bufHex("psection_data", &psection_data[offset], 32);
	ms_memcpy(bufOut, &psection_data[offset], len_section-LEN_SECTION_HEADER(psec_data->table_offset));
//ms_bufHex("bufOut", bufOut, (len_section-LEN_SECTION_HEADER(psec_data->table_offset)));
	return (len_section-LEN_SECTION_HEADER(psec_data->table_offset));
}
static ms_bool mpegts_innerapi_getSectionData_all(MPEGTSContxt *rlmpegts,SECTIONContext *psct_ctt,ms_u16 tablePid,
	ms_bool flag_dbg,ms_string destype)
{
	for(ms_u16 index=0;index<psct_ctt->total_num;index++){
		SECTIONData *psec_data=&psct_ctt->sec_data[index];		
		/*未接收完整的section_len长度*/
		//ms_enDbg(flag_dbg,"[%s]%u.len_recv,section_len:%u,%u",destype,index, psec_data->len_recv, psec_data->len_section);
		if(!mpegts_innerapi_recvOK(psec_data)){
			return ms_false;
		}else{
			psec_data->flag_recvOk=ms_true;
		}
		if(!psec_data->flag_recvOk){
			return ms_false;
		}	
	}

	ms_s32 table_offset=1;
	psct_ctt->len_recv=LEN_SECTION_HEADER(table_offset);
	for(ms_u16 index=0;index<psct_ctt->total_num;index++){
		SECTIONData *psec_data=&psct_ctt->sec_data[index];
		ms_u16 len_section=mpegts_innerapi_getSectionData_one(rlmpegts,psec_data, tablePid,
			&psct_ctt->data[psct_ctt->len_recv],flag_dbg,destype);
		if(0==len_section){
			psec_data->flag_recvOk=ms_false;
			psct_ctt->len_recv=0;
			return ms_false;
		}
		psct_ctt->len_recv+=len_section;
	}
	//ms_bufHex("psection_data", psct_ctt->data, psct_ctt->len_recv);
	mssi_api_buildSyntax1Head(&psct_ctt->data[table_offset], tablePid, psct_ctt->len_recv, psct_ctt->ts_id,psct_ctt->vrsion_num,0,0);
	
	return (psct_ctt->len_recv>0) ? ms_true : ms_false;
}
static ms_bool mpegts_innerapi_recvSection(MPEGTSContxt *rlmpegts,SECTIONContext *psct_ctt,ms_u16 tablePid,
	char *pes_buf,ms_string destype,ms_bool flag_dbg)
{
	SECTIONData *psec_data=&psct_ctt->sec_data[psct_ctt->section_index];
	ms_s32 table_offset=0;
	
/*如果是重组，则指向dataRecombine；否则，指向pes_buf*/	
	ms_byte *bufData=pes_buf;
	if(psct_ctt->flag_recombine){
		bufData=psct_ctt->dataRecombine;
		ms_memcpy(&psct_ctt->dataRecombine[TS_SIZE_PES], pes_buf, TS_SIZE_PES);
		goto msparse;
	}
	if(1==rlmpegts->headInfo.payload_unit_start_ind){
		psct_ctt->adaptation_field_len=mssi_api_getAdaptationFieldLen(rlmpegts->headInfo.adaptation_field_ctr, bufData);
	/*调整字段太长，导致无法解析有效数据，需要将2个PES进行组合*/			
		if(psct_ctt->adaptation_field_len>(TS_SIZE_PES-LEN_SECTION_HEADER(1))){
			ms_memcpy(psct_ctt->dataRecombine, bufData, TS_SIZE_PES);
			psct_ctt->flag_recombine=ms_true;
		}else{
msparse:	
		/*定位负载数据起始位置*/
			table_offset=mpegts_innerapi_isTargstTable(destype,rlmpegts, bufData,psct_ctt->adaptation_field_len ,tablePid, flag_dbg);
			if(table_offset<0){
				mpegts_innerapi_tableErr(rlmpegts, tablePid);
				goto ms_faild;
			}
			ms_s32 payload_offset=table_offset+psct_ctt->adaptation_field_len;
			ms_enDbg(flag_dbg,"[%s]payload_offset:%d,table_offset:%d",destype,payload_offset,table_offset);
			ms_byte *pes_payload=&bufData[payload_offset];
			
		/*获取分段的信息*/		
			ms_u16 len_section=((pes_payload[1]&0x0f)<<8)|(pes_payload[2]&0Xff);
			psct_ctt->ts_id=ms_host_2b(&pes_payload[3]);
			psct_ctt->vrsion_num=(pes_payload[5]&0x3E)>>1;
			ms_u08 current_next_indicator=pes_payload[5]&0x01;
			ms_u08 section_num=pes_payload[6];
			ms_u08 section_lastsec=pes_payload[7];
			ms_enDbg(flag_dbg,"[%s]section_len:%u",destype,len_section);
			ms_enDbg(flag_dbg,"[%s]ts_id:%u",destype,psct_ctt->ts_id);
			ms_enDbg(flag_dbg,"[%s]version_num:%u",destype,psct_ctt->vrsion_num);
			ms_enDbg(flag_dbg,"[%s]current_next_indicator:%u",destype,current_next_indicator);
			ms_enDbg(flag_dbg,"[%s]section_num:%u",destype,section_num);
			ms_enDbg(flag_dbg,"[%s]section_lastsec:%u",destype,section_lastsec);

		/*len_section长度异常*/	
			if(!len_section||len_section>MAX_SECTION_LEN){
				ms_error("[%s]section_len(%u)长度异常",destype,len_section);
				mpegts_innerapi_tableErr(rlmpegts, tablePid);
				goto ms_faild;
			}
			if((section_lastsec+1)>MAX_SECTION){
				ms_enWar(flag_dbg,"[%s]当前的有%u个section，超出范围0～%u",destype,(section_lastsec+1), MAX_SECTION);
				goto ms_faild;
			}
			psct_ctt->section_index=section_num;
			psec_data=&psct_ctt->sec_data[section_num];	
			/*只有没有接收完成，或者版本变化才进行重新接收*/
			if(!psec_data->flag_recvOk
				||psec_data->vrsion_num!=psct_ctt->vrsion_num){
				psct_ctt->total_num=section_lastsec+1;
				psec_data->adaptation_field_len=psct_ctt->adaptation_field_len;
				psec_data->len_section=len_section;
				psec_data->vrsion_num=psct_ctt->vrsion_num;
				psec_data->current_next_indicator=current_next_indicator;
				psec_data->section_num=section_num;
				psec_data->section_lastsec=section_lastsec;
				psec_data->table_offset=table_offset;
				ms_u16 len_recv=0;
				if(psct_ctt->flag_recombine){
					len_recv=TS_SIZE_PES*2;	
					ms_bufHex("bufData", bufData, len_recv);

				}else{
					len_recv=TS_SIZE_PES;
				}
				ms_memcpy(&psec_data->data[0], bufData, len_recv);	
				psec_data->len_recv=len_recv;
				psec_data->flag_recvOk=ms_false;
				psct_ctt->flag_recombine=ms_false;
				ms_enDbg(flag_dbg,"[%s]The psi has pointer_filed in the first byte(len_recv:%d, section_len:%d.section_index:%u)",
					destype,psec_data->len_recv, psec_data->len_section,psct_ctt->section_index);
			}
		}
	}else{
		//表长度过长，采用多个TS包传输
		if(!psec_data->flag_recvOk){
			ms_memcpy(&psec_data->data[psec_data->len_recv], bufData, TS_SIZE_PES);	
			psec_data->len_recv+=TS_SIZE_PES;	
		}
		ms_enDbg(flag_dbg,"[%s]The psi hasnot pointer_filed in the first byte(len_recv:%d, section_len:%d.section_index:%u)",destype,
			psec_data->len_recv, psec_data->len_section,psct_ctt->section_index);		
	}
/*只有接收完一个分段的时间点，才触发重新解析*/
	if(!psec_data->flag_recvOk&&mpegts_innerapi_recvOK(psec_data)){
		psec_data->flag_recvOk=ms_true;
		return mpegts_innerapi_getSectionData_all(rlmpegts, psct_ctt,tablePid,flag_dbg,destype);
	}
	return ms_false;
ms_faild:
	psct_ctt->len_recv=0;
	psct_ctt->flag_recombine=ms_false;	
	return ms_false;	
}
static ms_void mpegts_innerapi_recvSectionClean(SECTIONContext *psct_ctt)
{
	//ms_memset(psct_ctt, 0, ms_bufsize(SECTIONContext));
}
static ms_bool mpegts_innerapi_filterNullPkt(MPEGTSContxt *rlmpegts)
{
	return (SI_PID_NULL==rlmpegts->headInfo.pid) ? ms_true : ms_false;
}
static ms_bool mpegts_innerapi_filterPat(MPEGTSContxt *rlmpegts,char * tspkt)
{
	bufcheck_ret(ms_false,rlmpegts);
	bufcheck_ret(ms_false,tspkt);	
/*检测是否PAT表*/	
	if(PAT_PID!=rlmpegts->headInfo.pid){
		return ms_false;
	}
/*获取TAG*/		
	mpegts_innerapi_getTag(rlmpegts, "PAT", rlmpegts->paInfo.tag);	
/*更新发送时间*/	
	mpegts_innerapi_getPeriod(&rlmpegts->stream_info->period_pat);
/*检测PAT表的CC和发送间隔*/			
	if(mode_monitor&rlmpegts->mode){
		if(mssi_api_CCCheck(rlmpegts->paInfo.tag,&rlmpegts->stream_info->ctuInfo_pat,rlmpegts->headInfo.continuity_counter,ms_false,DBG_CC)<0){
			rlmpegts->stream_info->monitorInfo.errLevel1_ctt.ccErr+=1;
		}
		ms_enDbg(DBG_AV,"%s: cc[last]0x%02x", rlmpegts->paInfo.tag, rlmpegts->stream_info->ctuInfo_pat.last_cc);
		if(rlmpegts->stream_info->period_pat.period>ms_usmseconds(500)){
			rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
		}
	}
/*检测到传输错误*/		
	if(rlmpegts->headInfo.transporterror_indicator){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
		return ms_true;
	}
/*检测到被加密了*/		
	if(rlmpegts->headInfo.transport_scrambling_control){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
	}
	ms_bool flag_tmp=rlmpegts->flag_findPatInfo;
	ms_bool flag_dbg=flag_tmp ? DBG_MAP_PAT : DBG_PAT;
	PATTABLEInfo paInfo_tmp;
	ms_memset(&paInfo_tmp, 0, ms_bufsize(PATTABLEInfo));
	PATTABLEInfo *paInfo=flag_tmp ? &paInfo_tmp : &rlmpegts->paInfo;
	
/*接收section数据*/	
	char *pes_buf=&tspkt[TS_SIZE_HEADER];
	ms_bool flag_rev=mpegts_innerapi_recvSection(rlmpegts, &paInfo->sct_ctt,SI_TID_PAT,pes_buf,rlmpegts->paInfo.tag ,flag_dbg);

/*已接收完整的section数据，开始解析*/
	if(flag_rev){
	/*获取PES头部位置*/
		ms_u08 adaptation_field_len=0;
		ms_u16 len_data=paInfo->sct_ctt.len_recv;
		ms_byte *psection_data=paInfo->sct_ctt.data;
		ms_s32 table_offset=mpegts_innerapi_isTargstTable("PAT",rlmpegts, psection_data,adaptation_field_len ,SI_TID_PAT, flag_dbg);
		if(table_offset<0){
			rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
			goto msclean;
		}
		ms_s32 payload_offset=table_offset+adaptation_field_len;
		ms_byte *pes_payload=&psection_data[payload_offset];
		
	/*解析头部1*/	
		paInfo->transport_stream_id=ms_host_2b((&pes_payload[3]));
		ms_enDbg(flag_dbg,"transport_stream_id:%#x=%d",paInfo->transport_stream_id,paInfo->transport_stream_id);	

	/*解析pes_payload*/
		ms_u16 payload_len=(len_data-PAT_HEADER_SIZE-(payload_offset-adaptation_field_len));
		ms_u16 program_totalnum=payload_len/PAT_PROGRAM_SIZE;
		if(0!=(payload_len%PAT_PROGRAM_SIZE)){
			rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
			ms_fix("[%s]Maybe payload_len is error.payload_len,len_data,adaptation_field_len,payload_offset:%d,%d,%d,%d",rlmpegts->paInfo.tag,payload_len,len_data,adaptation_field_len,payload_offset);
			goto msclean;
		}
	
		PMTInfo pmtInfo_tmp[MAX_PROGRAM]={0};	
		paInfo->totalnum_program_real=program_totalnum;
		ms_enDbg(flag_dbg,"-------[PAT]program_loop,program_totalnum(payload_len:%d):%d----------",payload_len,program_totalnum);
		for(ms_u16 program_index=0; program_index<program_totalnum;program_index++){	
			char *pgm_offset=&pes_payload[PAT_HEADER_SIZE+PAT_PROGRAM_SIZE*program_index];
		/*获取节目信息：program_num和programid的对应关系*/
			ms_u16 program_num=ms_host_2b(pgm_offset);
			ms_u16 programid=(((((pgm_offset)[2]&0x1f)<<8)|(((pgm_offset)[3]&0xff)))&0xffff);
			if(0==program_num){
				paInfo->pid_nit=programid;
				ms_enDbg(flag_dbg,"pid_nit:%#x",paInfo->pid_nit);
			}else{
		/*设置节目信息表pmt_info*/		
				PMTInfo *pmt_info=flag_tmp ? &pmtInfo_tmp[paInfo->totalnum_program] : &rlmpegts->pmtInfo[paInfo->totalnum_program];
				if(ms_false==mssi_api_isValidPid(programid)){
					rlmpegts->stream_info->monitorInfo.errLevel1_ctt.patErr+=1;
					ms_errGoto(msclean,"[%s]unvalid programid %d,out of range(%d~%d)",rlmpegts->paInfo.tag,programid, SI_PID_MIN, SI_PID_MAX);
				}	
				ms_enDbg(flag_dbg,"pro_num(%d), pmt:%#x(%d)",program_num,programid,programid);
				pmt_info->index=paInfo->totalnum_program;
				pmt_info->program_num=program_num;
				pmt_info->pid=paInfo->pid_pmt[paInfo->totalnum_program]=programid;
				if(!flag_tmp){
					rlmpegts->flag_findPatInfo=ms_true;
					rlmpegts->flag_update=ms_true;
				}
				paInfo->totalnum_program++;
				if(MAX_PROGRAM==paInfo->totalnum_program){
					ms_warGoto(msend,"[%s]Only support %d program",rlmpegts->paInfo.tag,MAX_PROGRAM);
				}			
			}
		}

msend:	
/*检测节目信息是否发生变化*/		
		if(flag_tmp){
			if(paInfo_tmp.totalnum_program!=rlmpegts->paInfo.totalnum_program){
				rlmpegts->flag_reparse=ms_true;
				rlmpegts->stream_info->monitorInfo.errLevel3_ctt.infoChange+=1;
				ms_byte armInfo[1024]={0};
				ms_sprintfs(armInfo,"节目数量发生了变化（%d->%d），需要重新解析",
					paInfo_tmp.totalnum_program,rlmpegts->paInfo.totalnum_program);
				mpegts_innerapi_msg(rlmpegts->stream_info,armInfo);
			}else{
				for(ms_u16 index=0;index<paInfo->totalnum_program;index++){
					if(pmtInfo_tmp[index].pid!=rlmpegts->pmtInfo[index].pid
						||pmtInfo_tmp[index].program_num!=rlmpegts->pmtInfo[index].program_num){
						rlmpegts->flag_reparse=ms_true;
						rlmpegts->stream_info->monitorInfo.errLevel3_ctt.infoChange+=1;
						ms_byte armInfo[1024]={0};
						ms_sprintfs(armInfo,"节目信息发生了变化(pmt_pid:%d->%d,program_num:%d->%d)，需要重新解析",
							pmtInfo_tmp[index].pid,rlmpegts->pmtInfo[index].pid,
							pmtInfo_tmp[index].program_num,rlmpegts->pmtInfo[index].program_num);
						mpegts_innerapi_msg(rlmpegts->stream_info,armInfo);
					}
				}
			}
		}
msclean:		
		mpegts_innerapi_recvSectionClean(&paInfo->sct_ctt);
	}	
	return ms_true;
}
static ms_bool mpegts_innerapi_filterPmt(MPEGTSContxt *rlmpegts,ms_u08 *tspkt)
{
	bufcheck_ret(ms_false,rlmpegts);
	bufcheck_ret(ms_false,tspkt);
/*必须先获取PAT信息，再获取MPT表信息*/	
	if(ms_false==rlmpegts->flag_findPatInfo){
		return ms_false;
	}
/*检测是否PMT表*/
	if(ms_true!=mpegts_innerapi_isPmt_byPid( rlmpegts,rlmpegts->headInfo.pid)){
		return ms_false;
	}

/*检测到传输错误*/		
	if(rlmpegts->headInfo.transporterror_indicator){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.pmtErr+=1;
		return ms_true;
	}
/*检测到被加密了*/		
	if(rlmpegts->headInfo.transport_scrambling_control){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.pmtErr+=1;
	}	
/*查找头部位置*/	
	char *pes_buf=&tspkt[TS_SIZE_HEADER];	
	ms_u08 adaptation_field_len=mssi_api_getAdaptationFieldLen(rlmpegts->headInfo.adaptation_field_ctr, pes_buf);
	ms_s32 table_offset=mpegts_innerapi_isTargstTable("PMT",rlmpegts, pes_buf,adaptation_field_len ,SI_TID_PMT, ms_false);
	if(table_offset<0){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.pmtErr+=1;
		return ms_false;
	}
	ms_u08 payload_offset=table_offset+adaptation_field_len;
	char *pes_payload=&pes_buf[payload_offset];
	
/*确认节目*/ 
	ms_u16 program_num=ms_host_2b((&pes_payload[3]));
	if(ms_false==mssi_api_isValidProgramNum(program_num)){
		ms_errRet(ms_false,"[%s]unvalid program_num %d,out of range(%d~%d)",rlmpegts->tag,
			program_num, 0X0001, 0XFFFE);
	}
	//使用program_num是为了应对不同节目PMT PID相同的情况
	PMTInfo * pmt_info_n=mpegts_innerapi_findPmt( rlmpegts,program_num);
	if(ms_null==pmt_info_n){
		return ms_false;
	}
/*获取TAG*/		
	mpegts_innerapi_getTag(rlmpegts, "PMT", pmt_info_n->tag);	
/*更新发送时间*/	
	mpegts_innerapi_getPeriod(&rlmpegts->stream_info->program_info[pmt_info_n->index].period_pmt);
/*检测PMT表的CC和发送间隔*/			
	if(mode_monitor&rlmpegts->mode){
		if(rlmpegts->stream_info->totalnum_program>0){
			PROGRAMInfo *program_info=&rlmpegts->stream_info->program_info[pmt_info_n->index];
			if(mssi_api_CCCheck(pmt_info_n->tag,&program_info->ctuInfo_pmt,rlmpegts->headInfo.continuity_counter,ms_false,DBG_CC)){
				rlmpegts->stream_info->monitorInfo.errLevel1_ctt.ccErr+=1;
			}
			ms_enDbg(DBG_AV,"%s: cc[last]0x%02x", pmt_info_n->tag, program_info->ctuInfo_pmt.last_cc);
		}
		if(pmt_info_n->periodPmt.period>ms_usmseconds(500)){
			rlmpegts->stream_info->monitorInfo.errLevel1_ctt.pmtErr+=1;
		}
	}

	ms_bool flag_tmp=pmt_info_n->flag_findInfo;
	ms_bool flag_dbg=flag_tmp ? DBG_MAP_PMT : DBG_PMT;	
/*解析头部1*/		
	ms_enDbg(flag_dbg,"-------[PMT]HEADER INFO:%d byte----------",SI_PMTLEN_HEADER);
	if(1== rlmpegts->headInfo.payload_unit_start_ind){
		ms_enDbg(flag_dbg,"The psi has pointer_filed in the first byte ");
	}else{
		//PMT表长度过长，采用多个TS包传输，暂未支持
		ms_enDbg(flag_dbg,"The psi hasnot pointer_filed in the first byte:I dont support,fix me ");
	}	
	ms_u16 section_len=((pes_payload[1]&0x0f)<<8)|(pes_payload[2]&0xff);//HEADER+payload_offset+payload
	ms_u08 section_num=pes_payload[6];
	ms_u08 section_lastsec=pes_payload[7];
	ms_enDbg(flag_dbg,"program:%d=%#x",program_num,program_num);
	ms_enDbg(flag_dbg,"section_len:%d",section_len);
	ms_enDbg(flag_dbg,"section_num:%d",section_num);
	ms_enDbg(flag_dbg,"section_lastsec:%d",section_lastsec);	
/*避免解析对当前运行造成影响，使用临时数据*/	
	PMTInfo pmtInfo_tmp;	
	ms_memset(&pmtInfo_tmp, 0, ms_bufsize(PMTInfo));
	PMTInfo * pmt_info=flag_tmp ? &pmtInfo_tmp : pmt_info_n;
/*设置program_num和pid_pcr*/	
	pmt_info->program_num=program_num;
	pmt_info->tableInfo.pid_pcr=(((((pes_payload)[8]&0x1f)<<8)|(((pes_payload)[9]&0xff)))&0xffff);
	ms_enDbg(flag_dbg,"pid_pcr:%#x(%d)",pmt_info->tableInfo.pid_pcr,pmt_info->tableInfo.pid_pcr);
	if(ms_false==mssi_api_isValidPid(pmt_info->tableInfo.pid_pcr)){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.pmtErr+=1;
		ms_errRet(ms_true, "[%s]unvalid pid_pcr %d,out of range(%d~%d)",pmt_info_n->tag,pmt_info->tableInfo.pid_pcr, SI_PID_MIN, SI_PID_MAX);
	}
	ms_u16 program_info_len=(((((pes_payload)[10]&0xf)<<8)|(((pes_payload)[11]&0xff)))&0xffff);	
	ms_enDbg(flag_dbg,"-------[PMT]program_info_des_loop----------");
	ms_enDbg(flag_dbg,"program_info_len:%d",program_info_len);	
	if(program_info_len>0){
		//获取节目信息
	}
/*PMT数据使用多个TS包传输的情况，暂不支持*/
	if((section_len+adaptation_field_len)>TS_SIZE_PES){
		ms_errRet(ms_true, "[%s](section_len+adaptation_field_len)(%d) > %d，暂不支持",pmt_info_n->tag,(section_len+adaptation_field_len),TS_SIZE_PES);
	}
/*CRC校验*/	
	ms_u32 crc=ms_host_4b((&pes_buf[section_len+adaptation_field_len]));
	ms_enDbg(flag_dbg,"crc:%#x=%u",crc,crc);
	ms_u32 crc_calac=mscrc_api_CRC32(&pes_buf[1+adaptation_field_len], (section_len-1));
	if(crc!=crc_calac){
		rlmpegts->stream_info->monitorInfo.errLevel1_ctt.pmtErr+=1;
		rlmpegts->stream_info->monitorInfo.errLevel2_ctt.crcErr+=1;
		ms_bufHex("PMT", tspkt, TS_SIZE_BASE);
		mssi_api_headerPrint(&rlmpegts->headInfo,ms_true);
		ms_errRet(ms_true,"[%s]crc(%#x)!=crc_calac(%#x),maybe pes_payload is error",pmt_info_n->tag,crc,crc_calac);
	}
	
/*获取节目的ES信息：PID、streamtype、avType、esinfo_des*/		
	MAPContxt *pMap_ctt=mpegts_innerapi_findMap(rlmpegts, pmt_info_n->program_num,ms_false);
	ms_enDbg(flag_dbg,"-------[PMT]stream loop----------");
	ms_u16 payload_len=section_len-SI_PMTLEN_HEADER-(payload_offset-adaptation_field_len)-program_info_len;
	for(ms_s32 parse_len=0; parse_len<payload_len;){
		char *parase_offset=&pes_payload[SI_PMTLEN_HEADER+program_info_len+parse_len];
		ms_u08 streamtype=parase_offset[0];
		ms_u16 epid=(((((parase_offset)[1]&0x1f)<<8)|(((parase_offset)[2]&0xff)))&0xffff);
		if(ms_false==mssi_api_isValidPid(epid)){
			ms_errRet(ms_true,"[%s]unvalid epid %d,out of range(%d~%d)",pmt_info_n->tag,epid, SI_PID_MIN, SI_PID_MAX);
		}	
		ms_u16 len_esInfo=(((((parase_offset)[3]&0xf)<<8)|(((parase_offset)[4]&0xff)))&0xffff);
		ms_byte esinfo[1024]={0};
		if(len_esInfo>0){
			ms_verbose("Has esinfo,len=%d", len_esInfo);
			ms_memcpy(esinfo, &parase_offset[5], len_esInfo);
		}
		parse_len+=(5+len_esInfo);
	
		ENUMAVType avType=mpegts_innerapi_getAVType_byStreamtype(streamtype);
		if(pmt_info->tableInfo.num_es<MAX_ES){
			ESInfo *pesInfo=&pmt_info->tableInfo.esInfo[pmt_info->tableInfo.num_es];
			pesInfo->index_pmt=pmt_info->index;
			pesInfo->index_es=pmt_info->tableInfo.num_es;
			pesInfo->avType=avType;
			pesInfo->pid=epid;
			pesInfo->streamType=streamtype;
			pesInfo->len_esInfo=len_esInfo;
			ms_memcpy(pesInfo->esInfo, esinfo, len_esInfo);
			if(streamtype>streamtype_max){
				ms_sprintfs(pmt_info->tableInfo.esInfo[pmt_info->tableInfo.num_es].strStreamType,"%s",UNKNOW_TYPE);	
			}else{
				ms_sprintfs(pmt_info->tableInfo.esInfo[pmt_info->tableInfo.num_es].strStreamType,"%s",str_streamType[streamtype]);	
			}
			pmt_info->tableInfo.num_es+=1;
			if(pmt_info->tableInfo.pid_pcr==pesInfo->pid){
				if(avtype_video==pesInfo->avType){
					pmt_info->tableInfo.sharePid|=PCR_PIDSHARE_VIDEO;
				}else if(avtype_audio==pesInfo->avType){
					pmt_info->tableInfo.sharePid|=PCR_PIDSHARE_AUDIO;
				}else{
					pmt_info->tableInfo.sharePid|=PCR_PIDSHARE_OTHERES;
				}
			}
		}
		if(!flag_tmp){
			pmt_info->flag_findInfo=ms_true;
			rlmpegts->flag_update=ms_true;
		}
	/*进行ES的PID映射*/
		ms_bool flag_findMapEs=ms_false;
		if(pMap_ctt){
			for(ms_s32 indexEs=0;indexEs<pMap_ctt->es_num;indexEs++){
				//找到ES的PID
				if(pMap_ctt->es_pid_org[indexEs]==epid){
					if(pMap_ctt->flag_map_es[indexEs]){
						ms_enInfo(flag_dbg,"ES[0x%04x-%u]#%d: %s, %s, len_esInfo(%u),重新映射PMT中的ES的PID:%d->%d",
							epid,epid,pmt_info->tableInfo.num_es,mpegts_api_getAVType(avType),
							mpegts_api_getStreamType(streamtype),len_esInfo,epid,pMap_ctt->es_pid[indexEs]);
						parase_offset[1]&=(~0x1F);
						parase_offset[1]|=(pMap_ctt->es_pid[indexEs]>>8)&0x1F;
						parase_offset[2]=pMap_ctt->es_pid[indexEs]&0xFF;
						flag_findMapEs=ms_true;
					}
					break;
				}
			}
		}
		if(!flag_findMapEs){
			ms_enDbg(flag_dbg,"ES[0x%04x-%u]#%d: %s, %s, len_esInfo(%u)",epid,epid,pmt_info->tableInfo.num_es,
				mpegts_api_getAVType(avType),mpegts_api_getStreamType(streamtype),len_esInfo);
		}
	}
/*进行PCR的PID、PMT的PID和program_num映射*/
	if(pMap_ctt){
		//找到pcr_pid的PID
		if(pMap_ctt->flag_map_pcr_pid){
			ms_enInfo(flag_dbg,"重新映射PMT中的PCR的PID:%d->%d",pmt_info->tableInfo.pid_pcr,pMap_ctt->pcr_pid);
			pes_payload[8]&=(~0x1F);	
			pes_payload[8]|=(pMap_ctt->pcr_pid>>8)&0x1F;
			pes_payload[9]=pMap_ctt->pcr_pid&0xFF;
		}
		//重映射PID
		if(pMap_ctt->flag_map_pmt_pid){
			ms_enInfo(flag_dbg,"重映射PMT的PID：%d->%d",pmt_info->pid,pMap_ctt->pmt_pid);
				mssi_api_headerRemap(pMap_ctt->pmt_pid, tspkt);
		}
		//重映射program_num
		if(pMap_ctt->flag_map_program_num){
			ms_enInfo(flag_dbg,"重映射program_num：%d->%d",pmt_info->program_num,pMap_ctt->program_num);
			ms_network_2b((&pes_payload[3]), pMap_ctt->program_num);
		}
		//重计算CRC
		ms_u32 crc_calac_remap=mscrc_api_CRC32(&pes_buf[1+adaptation_field_len], (section_len-1));
		ms_network_4b((&pes_buf[section_len+adaptation_field_len]),crc_calac_remap);
	}

/*检测节目信息是否发生变化*/	
	if(flag_tmp){
		if(pmtInfo_tmp.program_num!=pmt_info_n->program_num
			||pmtInfo_tmp.tableInfo.pid_pcr!=pmt_info_n->tableInfo.pid_pcr
			||pmtInfo_tmp.tableInfo.num_es!=pmt_info_n->tableInfo.num_es){
			rlmpegts->flag_reparse=ms_true;
			rlmpegts->stream_info->monitorInfo.errLevel3_ctt.infoChange+=1;
			ms_byte armInfo[1024]={0};
			ms_sprintfs(armInfo,"节目信息发生了改变(program_num:%d->%d,pid_pcr:%d->%d,num_es:%d->%d)，需要重新解析",
				pmtInfo_tmp.program_num,pmt_info_n->program_num,
				pmtInfo_tmp.tableInfo.pid_pcr,pmt_info_n->tableInfo.pid_pcr,
				pmtInfo_tmp.tableInfo.num_es,pmt_info_n->tableInfo.num_es);
			mpegts_innerapi_msg(rlmpegts->stream_info,armInfo);
		}else{
			for(ms_u16 index=0;index<pmt_info_n->tableInfo.num_es;index++){
				if(pmtInfo_tmp.tableInfo.esInfo[index].pid!=pmt_info_n->tableInfo.esInfo[index].pid
					||pmtInfo_tmp.tableInfo.esInfo[index].streamType!=pmt_info_n->tableInfo.esInfo[index].streamType){
					rlmpegts->flag_reparse=ms_true;
					rlmpegts->stream_info->monitorInfo.errLevel3_ctt.infoChange+=1;
					ms_byte armInfo[1024]={0};
					ms_sprintfs(armInfo,"节目%u的ES信息发生了变化(pid:%d->%d,streamType:%s->%s)，需要重新解析",pmtInfo_tmp.program_num,
						pmtInfo_tmp.tableInfo.esInfo[index].pid,pmt_info_n->tableInfo.esInfo[index].pid,
						mpegts_api_getStreamType(pmtInfo_tmp.tableInfo.esInfo[index].streamType),
						mpegts_api_getStreamType(pmt_info_n->tableInfo.esInfo[index].streamType));
					mpegts_innerapi_msg(rlmpegts->stream_info,armInfo);
					break;
				}
			}
		}
	}
	return ms_true;
}
static ms_bool mpegts_innerapi_filterSdt(MPEGTSContxt *rlmpegts,char * tspkt)
{
	bufcheck_ret(ms_false,rlmpegts);
	bufcheck_ret(ms_false,tspkt);	
	SDTTABLEInfo *pSdtInfo=&rlmpegts->sdtInfo;	
/*检测是否SDT表*/		
	if(SI_PID_SDT!=rlmpegts->headInfo.pid){
		return ms_false;
	}	
/*获取TAG*/		
	mpegts_innerapi_getTag(rlmpegts, "SDT", pSdtInfo->tag);	
/*更新发送时间*/		
	mpegts_innerapi_getPeriod(&rlmpegts->stream_info->period_sdt);
	if(rlmpegts->stream_info->period_pat.period>ms_usseconds(1)){
		rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
	}
/*检测SDT表的CC*/	
	if(mode_monitor&rlmpegts->mode){
		if(mssi_api_CCCheck(pSdtInfo->tag,&rlmpegts->stream_info->ctuInfo_sdt,rlmpegts->headInfo.continuity_counter,ms_false,DBG_CC)){
			rlmpegts->stream_info->monitorInfo.errLevel1_ctt.ccErr+=1;
		}
		ms_enDbg(DBG_AV,"[%s]cc[last]0x%02x", pSdtInfo->tag, rlmpegts->stream_info->ctuInfo_sdt.last_cc);
	}
/*检测到传输错误*/		
	if(rlmpegts->headInfo.transporterror_indicator){
		rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
		return ms_true;
	}
	if(rlmpegts->headInfo.transport_scrambling_control){
		rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
	}	
/*检测是否已获取完成*/		
	if((pSdtInfo->sdt_num==rlmpegts->paInfo.totalnum_program)){
		return ms_true;
	}	
	ms_enDbg(DBG_SDT,"[%s]sdt_num,totalnum_program:%d,%d",pSdtInfo->tag,pSdtInfo->sdt_num,rlmpegts->paInfo.totalnum_program);

/*接收section数据*/
	char *pes_buf=&tspkt[TS_SIZE_HEADER];
	ms_bool flag_rev=mpegts_innerapi_recvSection(rlmpegts, &pSdtInfo->sct_ctt,SI_TID_SDT_ACT,pes_buf,pSdtInfo->tag ,DBG_SDT);

/*已接收完整的section数据，开始解析*/
	if(flag_rev){
		ms_enDbg(DBG_SDT, "len_recv:%u", pSdtInfo->sct_ctt.len_recv);
		//ms_bufHex("sct_ctt", &pSdtInfo->sct_ctt.data[9], pSdtInfo->sct_ctt.len_recv);
	/*获取PES头部位置*/
		ms_enDbg(DBG_SDT,"-------[SDT]HEADER INFO:%d byte----------",SI_SDTLEN_HEADER);
		ms_u08 adaptation_field_len=0;
		ms_u16 len_data=pSdtInfo->sct_ctt.len_recv;
		ms_byte *psection_data=pSdtInfo->sct_ctt.data;
		ms_s32 table_offset=mpegts_innerapi_isTargstTable("SDT",rlmpegts, psection_data,adaptation_field_len ,SI_TID_SDT_ACT, DBG_SDT);
		if(table_offset<0){
			rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
			goto msclean;
		}
		ms_s32 payload_offset=table_offset+adaptation_field_len;
		ms_byte *pes_payload=&psection_data[payload_offset];

	/*解析头部*/	
		pSdtInfo->transport_streamid=ms_host_2b(&pes_payload[3]);
		pSdtInfo->original_network_id=ms_host_2b(&pes_payload[8]);
		ms_enDbg(DBG_SDT,"transport_streamid:%d",pSdtInfo->transport_streamid);
		ms_enDbg(DBG_SDT,"original_network_id:%d",pSdtInfo->original_network_id);

	/*获取服务信息*/	
		ms_u16 payload_len=len_data-SI_SDTLEN_HEADER-(payload_offset-adaptation_field_len);
ms_enDbg(DBG_SDT,"-------[SDT]service_des_loop,len=%d----------",payload_len);
		for(ms_u16 parse_len=0; parse_len<payload_len;){
			char *parase_offset=&pes_payload[SI_SDTLEN_HEADER+parse_len];
			ms_u16  serviceid=ms_host_2b(&parase_offset[0]);
			ms_u16 des_loop_len=(((((parase_offset)[3]&0x0f)<<8)|(((parase_offset)[4]&0xff)))&0xffff);
			ms_enDbg(DBG_SDT,"des_loop_len,parse_len,payload_len:%d,%d,%d",des_loop_len,parse_len,payload_len);
			if(parse_len<payload_len){
				ms_u16 index=0;
			/*查找是否是已获取服务信息*/
				for(index=0; index<pSdtInfo->sdt_num;index++){
					SDTInfo *psdt_info=&pSdtInfo->sdt_info[index];
					//ms_debug("serviceid:%#x---%#x",serviceid,psdt_info->serviceid);
					if(serviceid==psdt_info->serviceid){
						ms_enDbg(DBG_SDT,"find serviceid(programe_num):%d",serviceid);
						break;
					}
				}
			/*新的服务信息*/	
				if(!(index<pSdtInfo->sdt_num)){
					if(ms_noLess(pSdtInfo->sdt_num, rlmpegts->paInfo.totalnum_program)){
						ms_enDbg(DBG_SDT,"sdt_num =totalnum_program:%d=%d",pSdtInfo->sdt_num, rlmpegts->paInfo.totalnum_program);
						rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
						goto msclean;
					}
					SDTInfo *psdt_info=&pSdtInfo->sdt_info[index];
					psdt_info->serviceid=serviceid;
				/*ServiceDesLoop2信息*/	
					char *pdes=&parase_offset[SI_SDTLEN_HEADER_ServiceDesLoop1];
					ms_byte descriptor_tag=pdes[0];
					ms_byte descriptor_len=pdes[1];
					psdt_info->service_type=pdes[2];
					ms_u08 len_spn=pdes[3];
					ms_u08 len_sn=pdes[4+len_spn];
					ms_enDbg(DBG_SDT,"descriptor_tag:%#x",descriptor_tag);
					ms_enDbg(DBG_SDT,"descriptor_len:%u",descriptor_len);
					ms_enDbg(DBG_SDT,"serviceid:%#x=%u",psdt_info->serviceid,psdt_info->serviceid);
					ms_enDbg(DBG_SDT,"len_spn:%u",len_spn);
					ms_enDbg(DBG_SDT,"len_sn:%u",len_sn);
					//ms_bufHexAscii("SDT", pdes, des_loop_len);
				//校验长度 
					if(des_loop_len!=(SI_SDTLEN_HEADER_ServiceDesLoop1+len_sn+len_spn)
						||(descriptor_len-3)!=(des_loop_len-SI_SDTLEN_HEADER_ServiceDesLoop1)){
						ms_enBufHex(DBG_SDT, "pdes", pdes, des_loop_len)
						if((descriptor_len-3)==(des_loop_len-SI_SDTLEN_HEADER_ServiceDesLoop1)){
							ms_enErr(DBG_SDT,"无效的des_loop_len(%d),也许len_spn和len_sn出现错误,len_spn,len_sn:%u,%u",
								des_loop_len,len_spn,len_sn);	
						}else{
							ms_enErr(DBG_SDT,"无效的des_loop_len(%d),也许des_loop_len出现错误,len_spn,len_sn:%u,%u"
								,des_loop_len,len_spn,len_sn);	
						}
						rlmpegts->stream_info->monitorInfo.errLevel3_ctt.sdtErr+=1;
						goto msclean;
					}
					pSdtInfo->sdt_num++;

					ms_enDbg(DBG_SDT,"---------%d/%d------------",pSdtInfo->sdt_num,rlmpegts->paInfo.totalnum_program);
				//获取service_provider_name		
					char *service_provider_name=&pdes[4];
					ms_buf0(psdt_info->service_provider_name);
					for(ms_u08 index=0;index<len_spn;index++){
						ms_u08 tempbuf[16]={0};
						ms_strcats(tempbuf, psdt_info->service_provider_name, "%c", service_provider_name[index]);
					}
				//获取service_name		
					char *service_name=&pdes[5+len_spn];
					ms_buf0(psdt_info->service_name);
					for(ms_u08 index=0;index<len_sn;index++){
						ms_u08 tempbuf[16]={0};
						ms_strcats(tempbuf, psdt_info->service_name, "%c", service_name[index]);
					}	
					ms_enDbg(DBG_SDT,"service_type:%s",mpegts_api_getServiceType(psdt_info->service_type));
					ms_enDbg(DBG_SDT,"service_provider_name:%s",psdt_info->service_provider_name);	
					ms_enDbg(DBG_SDT,"service_name:%s",psdt_info->service_name);	
					ms_enDbg(DBG_SDT,"-----------------------");
				}
			}
			parse_len+=(SI_SDTLEN_HEADER_ServiceDesLoop1+des_loop_len);
		}
		rlmpegts->flag_update=ms_true;
msclean:		
		mpegts_innerapi_recvSectionClean(&pSdtInfo->sct_ctt);
	}
	
	return ms_true;
}
static ms_void mpegts_innerapi_filterPcr2(MPEGTSContxt *rlmpegts,ms_u08 *tspkt,PMTInfo * pmt_info,ms_u32 index,
	ms_bool discontinuity_indicator,ms_bool pcr_flag,ms_s64 pcr)
{	
	ms_enDbg(DBG_PCR, "[%s-Pid%d]节目%u的PCR", rlmpegts->tag,rlmpegts->headInfo.pid,pmt_info->program_num);
/*获取PCR*/
	if(pcr_flag){
		ms_s64 pcr_pre=pmt_info->pcr;
		pmt_info->pcr=pcr;	
		ms_enDbg(DBG_PCR, "[%#x]pcr:%s,pcr_pre:%s",rlmpegts->headInfo.pid,ms_num2strS64(pmt_info->pcr),ms_num2strS64(pcr_pre));
		if(mode_monitor&rlmpegts->mode){
			if(0==index&&0!=pcr_pre&&0==discontinuity_indicator){
				ms_s64 pcrDIErr=ms_absS64(pcr_pre, pmt_info->pcr);
				if((ms_moreThan(pmt_info->pcr, pcr_pre)&&pcrDIErr>ms_usmseconds(100))
					||(ms_noMore(pmt_info->pcr, pcr_pre)&&pcrDIErr>ms_usseconds(30))){
					rlmpegts->stream_info->monitorInfo.errLevel2_ctt.pcrDIErr+=1;
				}
			}
		}
	//使用节目1的PCR来进行码率控制(如果是共用PCR的情况，只用第1个)		
		if(mode_bctPcr&rlmpegts->mode){
			if(0==index&&rlmpegts->pmtInfo[0].tableInfo.pid_pcr==rlmpegts->headInfo.pid){
				ms_enDbg(DBG_PCR, "[%#x]PCR:%s",rlmpegts->headInfo.pid,ms_num2strS64(pmt_info->pcr));
				mpegts_api_rateEmuLong(rlmpegts->tag,"pcr",&rlmpegts->sbrt_pcr,pmt_info->pcr,ms_true);	
			}
		}
	}		
/*更新发送时间*/		
	mpegts_innerapi_getPeriod(&rlmpegts->stream_info->program_info[pmt_info->index].period_pcr);
/*检测发送间隔*/
	if(mode_monitor&rlmpegts->mode){
		if(0==pmt_info->tableInfo.sharePid){
			if(mssi_api_CCCheck(rlmpegts->tag,&rlmpegts->stream_info->program_info[pmt_info->index].ctuInfo_pcr,
				rlmpegts->headInfo.continuity_counter,ms_false,DBG_CC)<0){
				rlmpegts->stream_info->monitorInfo.errLevel1_ctt.ccErr+=1;
			}
			ms_enDbg(DBG_PCR,"[%#x]PCR: cc[last]0x%02x",rlmpegts->headInfo.pid, rlmpegts->stream_info->ctuInfo_pat.last_cc);
		}		
		if(0==index&&pmt_info->periodPcr.period>ms_usmseconds(100)){
			rlmpegts->stream_info->monitorInfo.errLevel2_ctt.pcrIntervalErr+=1;
		}
	}
/*映射PCR的PID*/		
	if(rlmpegts->flag_map_pcr_pid){
		MAPContxt *pmap_ctt=mpegts_innerapi_findMap(rlmpegts, pmt_info->program_num,ms_false);
		if(pmap_ctt){
			if(pmap_ctt->flag_map_pcr_pid){
				ms_enInfo(DBG_MAP_PCR,"找到PCR的PID:%d->%d",pmt_info->tableInfo.pid_pcr,pmap_ctt->pcr_pid);
				mssi_api_headerRemap(pmap_ctt->pcr_pid, tspkt);
				if(!(pmap_ctt->pcr_es_index<0)){
					mssi_api_CCBuild2(tspkt, &pmap_ctt->es_cc[pmap_ctt->pcr_es_index]);
				}else{
					mssi_api_CCBuild2(tspkt, &pmap_ctt->pcr_cc);
				}
			}
		}
	}	
}
static ms_bool mpegts_innerapi_filterPcr(MPEGTSContxt *rlmpegts,ms_u08 *tspkt)
{	
	bufcheck_ret(ms_false,rlmpegts);
	bufcheck_ret(ms_false,tspkt);
/*判断是否是PCR*/
	char *pes_buf=&tspkt[TS_SIZE_HEADER];
	if(ms_true!=mssi_api_isPcrData(rlmpegts->headInfo, pes_buf)){
		return ms_false;
	}
/*检测到传输错误*/		
	if(rlmpegts->headInfo.transporterror_indicator){
		return ms_true;
	}	
/*解析PCR*/
	PMTINFOList pmtInfo_list;
	mpegts_innerapi_findPmt_byPcrPid(rlmpegts,rlmpegts->headInfo.pid,&pmtInfo_list);
	if(pmtInfo_list.num){
	/*解析PCR*/	
		char *adapt_field=&pes_buf[1];
	/*解析第一字节*/	
		ms_enDbg(DBG_PCR, "-------------------------------------------------------------");
		ms_bool discontinuity_indicator=(adapt_field[0]>>7)&0x1;//不连续状态指示器：指示系统时间基点和CC不连续性
		ms_bool random_access_indicator=(adapt_field[0]>>6)&0x1;//对于音视频，其访问点是音视频序列首部的第一字节
		ms_bool elementary_stream_priority_indicator=(adapt_field[0]>>5)&0x1;//优先级
		ms_bool pcr_flag=(adapt_field[0]>>4)&0x1;//获取PCR标记位
		ms_bool opcr_flag=(adapt_field[0]>>3)&0x1;//获取OPCR标记位
		ms_bool splcing_point_flag=(adapt_field[0]>>2)&0x1;//是否出现拼接点
		ms_bool transport_private_extension_flag=(adapt_field[0]>>1)&0x1;//是否有私有数据
		ms_bool adaptation_field_extension_flag=(adapt_field[0]>>0)&0x1;//是否存在调整字段扩展	
		ms_enDbg(DBG_PCR, "[%s-Pid%d]adapt_field[0]:%d", rlmpegts->tag,rlmpegts->headInfo.pid,adapt_field[0]);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]discontinuity_indicator:%d", rlmpegts->tag,rlmpegts->headInfo.pid,discontinuity_indicator);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]random_access_indicator:%d", rlmpegts->tag,rlmpegts->headInfo.pid,random_access_indicator);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]elementary_stream_priority_indicator:%u", rlmpegts->tag,rlmpegts->headInfo.pid,elementary_stream_priority_indicator);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]pcr_flag:%u", rlmpegts->tag,rlmpegts->headInfo.pid,pcr_flag);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]opcr_flag:%u", rlmpegts->tag,rlmpegts->headInfo.pid,opcr_flag);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]splcing_point_flag:%u", rlmpegts->tag,rlmpegts->headInfo.pid,splcing_point_flag);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]transport_private_extension_flag:%u", rlmpegts->tag,rlmpegts->headInfo.pid,transport_private_extension_flag);
		ms_enDbg(DBG_PCR, "[%s-Pid%d]adaptation_field_extension_flag:%u", rlmpegts->tag,rlmpegts->headInfo.pid,adaptation_field_extension_flag);
		ms_s64 pcr=0;
		if(pcr_flag){
			pcr=mssi_api_getPcr(adapt_field);
		}
		//ms_info("PCR的PID为%d，找到%u个节目", rlmpegts->headInfo.pid,pmtInfo_list.num);
		for(ms_u32 index=0;index<pmtInfo_list.num;index++){
			mpegts_innerapi_filterPcr2(rlmpegts,tspkt, pmtInfo_list.pmtInfo[index], index, discontinuity_indicator,pcr_flag,pcr);
		}
	}else{
		ms_enDbg(DBG_PCR, "[%s-Pid%d]不属于任何节目的PCR", rlmpegts->tag,rlmpegts->headInfo.pid);
		/*不属于任何节目的PCR*/
		ESInfo *pEsInfo=mpegts_innerapi_findEs(rlmpegts);
		if(ms_null!=pEsInfo){
			PROGRAMInfo *program_info=&rlmpegts->stream_info->program_info[pEsInfo->index_pmt];
			program_info->esInfo[pEsInfo->index_es].numPcr_noInPmt+=1;
		}
	}
	return ms_true;
}

/*			   |PES|
|PES包包头|PES包头可选字段|PES包负载|*/
static void mpegts_innerapi_filterPes(MPEGTSContxt *rlmpegts,ESInfo *pEsInfo,char * pesPkt,ms_bool flag_btc)
{
/*payload_unit_start_ind标识一个新的PES*/	
	ms_s32 len=TS_SIZE_PES;
	char * pes_pkt=ms_null;
	//ms_enDbg(DBG_PES, "[%#x]payload_unit_start_ind:%u",pEsInfo->pid,rlmpegts->tsHeadInfo.payload_unit_start_ind);
	if(1==rlmpegts->headInfo.payload_unit_start_ind){
		ms_s32 index=pes_find(pesPkt,len);
		if(index<0){
			pEsInfo->pes_data_recvd+=len;
			//ms_bufHex("pesPkt", pesPkt, len);
			return;
		}else{
			if(pEsInfo->pes_data_recvd>0){
				ms_u32 num_tsPkt=(pEsInfo->pes_data_recvd/len);
				if(!(pEsInfo->pes_data_recvd%len)){
					num_tsPkt+=1;
				}
				ms_enDbg(DBG_PES, "[Pid-%#x]总接收长度:%u,大约%u个TS包",pEsInfo->pid,pEsInfo->pes_data_recvd,num_tsPkt);
			}
			ms_enDbg(DBG_PES, "[Pid-%#x]---------------------------新的PES头，偏移%d",pEsInfo->pid,index);
			pes_pkt=&pesPkt[index];
			len-=index;
		}
	}else{
		pEsInfo->pes_data_recvd+=len;
		return;
	}	
/*			|PES包包头|
|24位开始码|8位流ID|16位包长度|*/
	//开始码，固定为0x000001
	ms_u32 packet_start_code_prefix=ms_host_3b(pes_pkt);
	if(!pes_validate(pes_pkt)) {
		return;
	}
	ms_enDbg(DBG_PES, "[Pid-%#x]开始码:0x000001",pEsInfo->pid);
	pEsInfo->pes_data_recvd=0;
	//流ID，标识该PES包中包含的数据类型。例如音频、视频或其他数据流
	ms_u08 pes_streamid=pes_get_streamid(pes_pkt);
	mpegts_innerapi_showPesStreamId(pEsInfo,pes_streamid);
	
	PROGRAMInfo *program_info=&rlmpegts->stream_info->program_info[pEsInfo->index_pmt];
	//包长度，包括头部和数据，单位为字节。如果为0,则表示该PES是一个无界长度的数据流
	ms_u16 pes_length=pes_get_length(pes_pkt);
	ms_enDbg(DBG_PES, "[Pid-%#x]包长度:%u",pEsInfo->pid,pes_length);
/*			|PES包头可选字段|
|2位比特标记|2位数据加扰标记|1位优先权标记|1位数据对齐标志|1位版权|1位原始或拷贝标记|
|2位是否包含PTS和DTS标记||1位是否包含ESCR标记|1位是否包含ES码率标记|1位DSM trick mode标记|1位Additional copy info标记|1位PES CRC标记|1位PES externsion标记|
|8位PES头长度|--任选长度--|--填整字节(0xFF)--|

									|--任选长度--|
|33位PTSDTS|42位ESCR|22位ES速率|8位DSM特技方式|7位附加的复制信息|16位前PES CRC|PES扩展|

	|PES扩展|
|5个标志|--任选字段--|

									|--任选字段--|
|128位PES专用数据|8位包头字段|8位节目包顺序控制|16位P-STD扩展字节长度|PES扩展字段数据|*/	
	if(!pes_validate_header(pes_pkt)) {
		ms_errNoret("[Pid-%#x]比特标记:错误,不是0x80而是%#x",pEsInfo->pid,(pes_pkt[6] & 0xc0));
	}
	if(pes_get_dataalignment(pes_pkt)) {
		ms_enDbg(DBG_PES, "[Pid-%#x]数据对齐:是",pEsInfo->pid);
	}
	if(pes_has_pts(pes_pkt)&&pes_validate_pts(pes_pkt)){
		pEsInfo->pts=mstime_api_ts2sys(pes_get_pts(pes_pkt)); //使用90Khz的时钟单位，1PTS表示1/90000秒
		pEsInfo->flag_pts=ms_true;
		if(0!=pEsInfo->pts_pre){
			ms_s64 cha_pts=pEsInfo->pts-pEsInfo->pts_pre;
			if(ms_noMore(pEsInfo->pts,pEsInfo->pts_pre)){
				ms_enWar((DBG_PES_PTSDTS), "[Pid-%#x]PTS:%s us(上次%s us,差值%s us,非线性PTS)",pEsInfo->pid,
					ms_num2strU64(pEsInfo->pts),ms_num2strU64(pEsInfo->pts_pre),ms_num2strS64(cha_pts));
				program_info->esInfo[pEsInfo->index_es].numNonmomo[0]+=1;
			}else{
				ms_enDbg((DBG_PES_PTSDTS), "[Pid-%#x]PTS:%s us(上次%s us,差值%s us)",pEsInfo->pid,
					ms_num2strU64(pEsInfo->pts),ms_num2strU64(pEsInfo->pts_pre),ms_num2strS64((cha_pts)));
				if(flag_btc){
					mpegts_api_rateEmuLong(rlmpegts->tag,"pts",&rlmpegts->sbrt_pts,pEsInfo->pts,ms_true);
				}
			}

			if(mode_monitor&rlmpegts->mode){
				/*pcr大于pcr_pre且大于70ms;pcr不大于pcr_pre且小于1分钟*/
				if(0!=pEsInfo->pts_pre){
					if((ms_moreThan(pEsInfo->pts, pEsInfo->pts_pre)&&cha_pts>ms_usmseconds(700))
						||(ms_noMore(pEsInfo->pts, pEsInfo->pts_pre)&&cha_pts>ms_usseconds(30))){
						ms_enWar(ms_true, "[Pid-%#x]PTS:%s us(上次%s us,差值%s us)",pEsInfo->pid,
							ms_num2strU64(pEsInfo->pts),ms_num2strU64(pEsInfo->pts_pre),ms_num2strS64((cha_pts)));
						rlmpegts->stream_info->monitorInfo.errLevel2_ctt.ptsErr+=1;
					}
				}
			}
		}
		pEsInfo->pts_pre=pEsInfo->pts;
	}else{
		ms_enDbg(DBG_PES, "[Pid-%#x]PTS:无效",pEsInfo->pid);
		pEsInfo->pts=0;
	}
	if(pes_has_dts(pes_pkt)&&pes_validate_dts(pes_pkt)){
		pEsInfo->dts=mstime_api_ts2sys(pes_get_dts(pes_pkt));//使用90Khz的时钟单位，1PTS表示1/90000秒
		pEsInfo->flag_dts=ms_true;
		if(0!=pEsInfo->dts_pre){
			ms_s64 cha_dts=pEsInfo->dts-pEsInfo->dts_pre;
			if(ms_noMore(pEsInfo->dts,pEsInfo->dts_pre)){
				ms_enWar((DBG_PES_PTSDTS), "[Pid-%#x]DTS:%s us(上次%s us,差值%s us,非线性DTS)",pEsInfo->pid,
					ms_num2strU64(pEsInfo->dts),ms_num2strU64(pEsInfo->dts_pre),ms_num2strS64(cha_dts));
				program_info->esInfo[pEsInfo->index_es].numNonmomo[1]+=1;
			}else{
				ms_enDbg((DBG_PES_PTSDTS), "[Pid-%#x]DTS:%s us(上次%s us,差值%s us)",pEsInfo->pid,
					ms_num2strU64(pEsInfo->dts),ms_num2strU64(pEsInfo->dts_pre),ms_num2strS64(cha_dts));
				if((flag_btc)&&(ms_false==pEsInfo->flag_pts)){
					mpegts_api_rateEmuLong(rlmpegts->tag,"dts",&rlmpegts->sbrt_dts,pEsInfo->dts,ms_true);
				}
			}
		}
		pEsInfo->dts_pre=pEsInfo->dts;
	}else{
		ms_enDbg(DBG_PES, "[Pid-%#x]DTS:无效",pEsInfo->pid);
		pEsInfo->dts=0;
	}	
	ms_u16 pes_headerlength=pes_get_headerlength(pes_pkt);
	ms_u16 pes_headerlength_total=(PES_HEADER_SIZE +PES_HEADER_OPTIONAL_SIZE+pes_headerlength);
	ms_enDbg(DBG_PES, "[Pid-%#x]头长度:%u，头总长度为:%u",pEsInfo->pid,pes_headerlength,pes_headerlength_total);
/*PES包负载，包含音频或视频数据*/
	pEsInfo->pes_data_recvd+=(len-pes_headerlength_total);
	if(pes_length>0){
		pEsInfo->pes_data_len=pes_length-pes_headerlength_total;
		ms_enDbg(DBG_PES, "[Pid-%#x]数据长度:%u,pes_data_recvd:%u",pEsInfo->pid,pEsInfo->pes_data_len,pEsInfo->pes_data_recvd);
	}else{
		pEsInfo->pes_data_len=0;
		ms_enDbg(DBG_PES, "[Pid-%#x]数据长度:无界长度,pes_data_recvd:%u",pEsInfo->pid,pEsInfo->pes_data_recvd);
	}
}
static ms_bool mpegts_innerapi_filterEs(MPEGTSContxt *rlmpegts,char * tspkt,ms_bool is_pcr,ms_u16 *pid)
{
	bufcheckret_des(ms_false,rlmpegts,"param err:rlmpegts");
	bufcheckret_des(ms_false,tspkt,"param err:tspkt");
	
	char *pesBuf=&tspkt[TS_SIZE_HEADER];
	PMTInfo *pmt_info=mpegts_innerapi_findPmt_byEsPid(rlmpegts,rlmpegts->headInfo.pid);

/*如果PCR和ES的PID相同，此处可能会把PCR数据透下去*/	
	ESInfo *pEsInfo=mpegts_innerapi_findEs(rlmpegts);
	if(ms_null==pEsInfo){
		return ms_false;
	}
	
	PROGRAMInfo *program_info=&rlmpegts->stream_info->program_info[pmt_info->index];
	if((mode_monitor&rlmpegts->mode) ){
	/*更新发送时间*/		
		mpegts_innerapi_getPeriod(&program_info->esInfo[pEsInfo->index_es].period);
		mpegts_innerapi_getTag(rlmpegts, pEsInfo->strStreamType, pEsInfo->tag);
		if(ms_true!=is_pcr){//一些流的音视频和PCR的PID相同
			if(mssi_api_CCCheck(pEsInfo->tag,&program_info->esInfo[pEsInfo->index_es].ctuInfo,rlmpegts->headInfo.continuity_counter,ms_true,DBG_CC)<0){
				rlmpegts->stream_info->monitorInfo.errLevel1_ctt.ccErr+=1;
				//ms_info("avType:%d", pEsInfo->avType);
				if(avtype_video==pEsInfo->avType||avtype_audio==pEsInfo->avType){
					rlmpegts->stream_info->monitorInfo.errLevel1_ctt.ccErr_av+=1;
				}
				if(DBG_CC&&(DBG_CC_PID==rlmpegts->headInfo.pid)){
					mssi_api_headerPrint(&rlmpegts->headInfo,1);
					ms_bufHex("table_buf", pesBuf, TS_SIZE_PES);
					ms_exit(1);
				}
			}else{
				if(DBG_CC&&(DBG_CC_PID==rlmpegts->headInfo.pid)){
					mssi_api_headerPrint(&rlmpegts->headInfo,1);
					ms_bufHex("table_buf", pesBuf, TS_SIZE_PES);
				}
			}
		}else{
			program_info->esInfo[pEsInfo->index_es].ctuInfo.last_cc_pcr=rlmpegts->headInfo.continuity_counter;
		}
		if(ms_true!=is_pcr){
			ms_enDbg(DBG_AV,"%s: cc[last]0x%02x", pEsInfo->tag, program_info->esInfo[pEsInfo->index_es].ctuInfo.last_cc);
		}else{
			ms_enDbg(DBG_AV,"%s: cc[last]0x%02x,is_pcr:%d", pEsInfo->tag,program_info->esInfo[pEsInfo->index_es].ctuInfo.last_cc_pcr,is_pcr);
		}
	}

	if(ms_true==is_pcr){
		return ms_false;
	}
/*检测到传输错误*/		
	if(rlmpegts->headInfo.transporterror_indicator){
		return ms_true;
	}	
	//ms_info("mode:%d", mode);
	if( (mode_bctPtsDts&rlmpegts->mode) && (ms_null!=rlmpegts->pEsInfo_btcPtsDts&&rlmpegts->pEsInfo_btcPtsDts==pEsInfo) ){
		mpegts_innerapi_filterPes(rlmpegts,pEsInfo,pesBuf,ms_true);
	}else if(mode_parsePes&rlmpegts->mode ){
		mpegts_innerapi_filterPes(rlmpegts,pEsInfo,pesBuf,ms_false);
	}
	
	MAPContxt *pmap_ctt=mpegts_innerapi_findMap(rlmpegts, pmt_info->program_num,ms_false);
	if(pmap_ctt){
		/*进行PID映射*/
		if(rlmpegts->flag_map_es){
			for(ms_s32 indexEs=0;indexEs<pmap_ctt->es_num;indexEs++){
				if(pmap_ctt->es_pid_org[indexEs]==pEsInfo->pid&&pmap_ctt->flag_map_es[indexEs]){
					ms_enInfo(DBG_MAP_ES,"找到ES(%s)，重新映射ES的PID:%d->%d，重新映射ES的CC:%d->%d",pEsInfo->strStreamType,
						pEsInfo->pid,pmap_ctt->es_pid[indexEs],
						rlmpegts->headInfo.continuity_counter,pmap_ctt->es_cc[indexEs]);
					mssi_api_headerRemap(pmap_ctt->es_pid[indexEs], tspkt);
				}
			}
		}
		/*PCR和ES的PID可能相同，若将PCR分离，则会出现CC丢失的问题，因此重新进行CC计数*/
		for(ms_s32 indexEs=0;indexEs<pmt_info->tableInfo.num_es;indexEs++){
			if(pEsInfo->pid==pmt_info->tableInfo.esInfo[indexEs].pid){
				ms_enInfo(DBG_MAP_ES,"找到ES(%s)重新映射ES的CC:%d->%d",pEsInfo->strStreamType,
					rlmpegts->headInfo.continuity_counter,pmap_ctt->es_cc[indexEs]);
				mssi_api_CCBuild2(tspkt, &pmap_ctt->es_cc[indexEs]);
				break;
			}
		}
	}	
	return ms_true;
}
static ms_s08 mpegts_innerapi_init2(MPEGTSContxt *rlmpegts)
{
	ms_bufcheckRet(-1, rlmpegts);
	ms_strcpy(rlmpegts->stream_info->tag,rlmpegts->tag);
	
	if(rlmpegts->mapList_ctt){
		if(rlmpegts->mapList_ctt->num_map>0){
			for(ms_s32 iMap=0;iMap<rlmpegts->mapList_ctt->num_map;iMap++){
				MAPContxt *pmap_ctt=&rlmpegts->mapList_ctt->map_ctt[iMap];
				if(mssi_api_isValidProgramNum(pmap_ctt->program_num_org)
					&&mssi_api_isValidProgramNum(pmap_ctt->program_num)
					&&pmap_ctt->program_num_org!=pmap_ctt->program_num){
					rlmpegts->flag_map_program_num=ms_true;
					pmap_ctt->flag_map_program_num=ms_true;
				}
				if(mssi_api_isValidPid(pmap_ctt->pmt_pid)){
					rlmpegts->flag_map_pmt_pid=ms_true;
					pmap_ctt->flag_map_pmt_pid=ms_true;
				}
				if(mssi_api_isValidPid(pmap_ctt->pcr_pid)){
					rlmpegts->flag_map_pcr_pid=ms_true;
					pmap_ctt->flag_map_pcr_pid=ms_true;
				}
				pmap_ctt->pcr_es_index=-1;
				//ms_info("pmap_ctt->es_num:%d",pmap_ctt->es_num);
				for(ms_s32 iEs=0;iEs<pmap_ctt->es_num;iEs++){
					//ms_info("%d %#x=%#x",iEs,pmap_ctt->es_pid_org[iEs],pmap_ctt->es_pid[iEs]);
					if(mssi_api_isValidPid(pmap_ctt->es_pid_org[iEs])
						&&mssi_api_isValidPid(pmap_ctt->es_pid[iEs])
						&&pmap_ctt->es_pid_org[iEs]!=pmap_ctt->es_pid[iEs]){
						rlmpegts->flag_map_es=ms_true;
						pmap_ctt->flag_map_es[iEs]=ms_true;
						if(pmap_ctt->pcr_pid==pmap_ctt->es_pid[iMap]){
							pmap_ctt->pcr_es_index=iMap;
						}
					}
				}
				if(ms_buflen(pmap_ctt->service_name)&&ms_buflen(pmap_ctt->service_provider_name)){
					rlmpegts->flag_map_name=ms_true;
					pmap_ctt->flag_map_name=ms_true;
				}
			}
		}
	}

	if(mode_rebuildPatPmt&rlmpegts->mode||rlmpegts->flag_map_program_num||rlmpegts->flag_map_pmt_pid){
		rlmpegts->stream_info->flag_rebuild_pat=ms_true;
	}
	if(mode_rebuildPatPmt&rlmpegts->mode){
		rlmpegts->stream_info->flag_rebuild_pmt=ms_true;
	}
	if(rlmpegts->flag_map_program_num||rlmpegts->flag_map_name){
		rlmpegts->stream_info->flag_rebuild_sdt=ms_true;
	}
	return 0;
}
ms_bool mpegts_api_isPidErr(STREAMInfo *stream_info,ms_s64 sys_cur,ms_s64 sys_pre,ms_string armInfo)
{	
/*sys_pre为0：表示没有出现过
((sys_cur-sys_pre)>ms_usminutes(5))：表示超过5分钟未出现*/
	if(!sys_pre||((sys_cur-sys_pre)>ms_usminutes(5))){
		if(sys_pre){
			mpegts_innerapi_msg(stream_info,armInfo);
		}
	}
	return (!sys_pre||((sys_cur-sys_pre)>ms_usminutes(5))) ? ms_true  : ms_false;
}
ms_void mpegts_api_getPidErr(void *stream_info_p)
{
	ms_bufcheckNoret(stream_info_p);
	STREAMInfo *stream_info=(STREAMInfo *)stream_info_p;
	if(stream_info->progress<100){
		return;	//未解析完成
	}
	stream_info->monitorInfo.errLevel1_ctt.pidErr=0;
	stream_info->nb_streams=0;
	stream_info->nb_video=0;
	stream_info->nb_audio=0;

	ms_u64 sys_cur=mstime_api_upUs();
/*查找PAT是否丢失*/	
	ms_byte armInfo[1024]={0};
	ms_strcpy(armInfo,"超过5分钟未收到PAT表");
	if(mpegts_api_isPidErr(stream_info, sys_cur, stream_info->period_pat.sys_pre, armInfo)){
		stream_info->monitorInfo.errLevel1_ctt.pidErr|=lost_pat;
	}
/*查找节目关键PID是否丢失*/	
	for(ms_s32 index=0;index<stream_info->totalnum_program;index++){
		PROGRAMInfo *program_info=&stream_info->program_info[index];
		/*查找PMT是否丢失*/
		ms_sprintfs(armInfo,"超过5分钟未收到节目%04u的PMT表",program_info->program_num);
		if(mpegts_api_isPidErr(stream_info, sys_cur, program_info->period_pmt.sys_pre, armInfo)){
			stream_info->monitorInfo.errLevel1_ctt.pidErr|=lost_pmt;	
		}
		/*查找音视频是否丢失*/
		for(ms_s32 indexEs=0;indexEs<program_info->num_es;indexEs++){
			stream_info->nb_streams+=1;
			ESInfo *esInfo=&program_info->esInfo[indexEs];
			if(avtype_video==esInfo->avType){
				stream_info->nb_video+=1;
			}else if(avtype_audio==esInfo->avType){
				stream_info->nb_audio+=1;
			}
			ms_sprintfs(armInfo,"超过5分钟未收到节目%04u的%s数据",program_info->program_num,mpegts_api_getAVType(esInfo->avType));
			if(mpegts_api_isPidErr(stream_info, sys_cur, esInfo->period.sys_pre, armInfo)){
				if(avtype_video==esInfo->avType){
					stream_info->monitorInfo.errLevel1_ctt.pidErr|=lost_video;
				}else if(avtype_audio==esInfo->avType){
					stream_info->monitorInfo.errLevel1_ctt.pidErr|=lost_audio;
				}else if(avtype_subtitle==esInfo->avType){
					stream_info->monitorInfo.errLevel1_ctt.pidErr|=lost_subtitle;
				}else{
					stream_info->monitorInfo.errLevel1_ctt.pidErr|=lost_esOther;
				}
			}
		}
		/*查找PCR是否丢失*/
		ms_sprintfs(armInfo,"超过5分钟未收到节目%04u的PCR数据",program_info->program_num);
		if(mpegts_api_isPidErr(stream_info, sys_cur, program_info->period_pcr.sys_pre, armInfo)){
			stream_info->monitorInfo.errLevel1_ctt.pidErr|=lost_pcr;	
		}
	}
}
ms_void mpegts_api_getPidInfo(PIDLISTContxt *pidList_ctt,PIDContxt *pbr_ctt,void *stream_info_p)
{
	ms_bufcheckNoret(stream_info_p);
	ms_bufcheckNoret(pidList_ctt);
	ms_bufcheckNoret(pbr_ctt);
	STREAMInfo *stream_info=(STREAMInfo *)stream_info_p;
	if(pbr_ctt->flag_use){
		if(!ms_buflen(pbr_ctt->info)){					
			if(SI_PID_PAT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "PAT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_CAT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "CAT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_TSDT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "TSDT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_NIT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "NIT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_SDT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "SDT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_BAT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "BAT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_EIT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "EIT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_TDT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "TDT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_TOT==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "TOT");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else if(SI_PID_NULL==pbr_ctt->pid){
				ms_strcpy(pbr_ctt->info, "null packets");
				pbr_ctt->program_num=SI_PID_UNVALID;
			}else{
				for(ms_s32 program_index=0;program_index<stream_info->totalnum_program;program_index++){
					PROGRAMInfo *program_info=&stream_info->program_info[program_index];
					ms_bool flag_pcr=ms_false;
					if(program_info->pmt_pid==pbr_ctt->pid){
						ms_sprintfs(pbr_ctt->info, "PMT(%04u)",program_info->program_num);
						pbr_ctt->program_num=program_info->program_num;
					}else if(program_info->pcr_pid==pbr_ctt->pid){
						if(program_info->sharePid&PCR_PIDSHARE_PROGRAM){
							ms_byte info[512]={0};
							ms_byte tbuf[512]={0};
							for(ms_u32 index=0;index<program_info->sharePid_num;index++){
								if(index){
									ms_strcats(tbuf, info, "+%04u",program_info->sharePid_program_num[index]);
								}else{
									ms_strcats(tbuf, info, "%04u",program_info->sharePid_program_num[index]);
								}
							}
							ms_sprintfs(pbr_ctt->info, "PCR(%s)",info);
						}else{
							ms_sprintfs(pbr_ctt->info, "PCR(%04u)",program_info->program_num);
						}		
						pbr_ctt->program_num=program_info->program_num;
						flag_pcr=ms_true;
					}
					for(ms_s32 es_index=0;es_index<program_info->num_es;es_index++){
						ESInfo *pMEsInfo=&program_info->esInfo[es_index];
						if(pMEsInfo->pid==pbr_ctt->pid){
							pbr_ctt->program_num=program_info->program_num;
							if(flag_pcr){
								ms_sprintfs(pbr_ctt->info, "%s-%s+pcr(%04u)",mpegts_api_getAVType(pMEsInfo->avType),
									pMEsInfo->strStreamType,program_info->program_num);
							}else{
								ms_sprintfs(pbr_ctt->info, "%s-%s(%04u)",mpegts_api_getAVType(pMEsInfo->avType),
									pMEsInfo->strStreamType,program_info->program_num);
							}
						}
					}
				}
			} 
		}
		if(pidList_ctt->totalPid>1000){
			ms_u32 hW=pbr_ctt->num*100/pidList_ctt->totalPid;
			ms_u32 lW=(pbr_ctt->num*100%pidList_ctt->totalPid)*1000/pidList_ctt->totalPid;
			ms_sprintfs(pbr_ctt->percentage, "%02u.%03u", hW,lW);
			msbitrate_api_clac(&pbr_ctt->streamrate_info,0, flags_bitrate_realtime|flags_bitrate_forceClac);
		}
	}
}
/*非实时更新时间基数：可以避免单次异常延时导致的整体延迟,因为始终保持ctime_sys等于ctime_stream*/	
ms_s64 mpegts_api_rateEmuLong(char * tag,char * exStr,RATEEMUContxt * sbrt,ms_s64 timeStream_cur,ms_bool flag_dbg)
{
	ms_s64 timeSys_cur=mstime_api_upUs();
	if(ms_true!=mpegts_innerapi_isRateEmuVaild(sbrt, timeStream_cur, timeSys_cur)){
		goto update;
	}
	mpegts_innerapi_rateEmuDone(tag, exStr, sbrt, timeStream_cur, timeSys_cur,flag_dbg);
	return timeSys_cur;
update:		
	if(timeStream_cur>0){
		sbrt->num_failed=0;
		sbrt->timeStream_pre=timeStream_cur;
		sbrt->timeSys_pre=mstime_api_upUs();
	}
	return -1;
}
ms_bool mpegts_api_getStreamInfo(STREAMInfo stream_info,char *outbuf,ms_bool flag_format)
{
	//ms_debug("program_totalnum:%d",stream_info.program_totalnum);
	ms_u08 program_index=0;
	ms_u08 tempbuf[1024];
	ms_u08 info[512]={0};
	if(flag_format&&ms_buflen(stream_info.format)>0){
		ms_strcat(info,stream_info.format);
	}
	if(mpegts_api_isMpeg(stream_info.format,stream_info.program_info[0].pmt_pid)){
		if(stream_info.pktSize>0){
			if(ms_buflen(info)){ms_strcat(info,",");}
			ms_strcats(tempbuf,info,"%u",stream_info.pktSize);
		}
		if(ms_range2(stream_info.progress, 0, 100)){
			if(ms_buflen(info)){ms_strcat(info,",");}
			ms_strcats(tempbuf,info,"progress(%u)",stream_info.progress);
		}
	}
	MS_STRCATS_STR(tempbuf, outbuf, "info[%s]", info);
	ERRLEVEL1Contxt *perr1_ctt=&stream_info.monitorInfo.errLevel1_ctt;
	MS_STRCATS_INT(tempbuf,outbuf,",syncLost[%u]",perr1_ctt->syncLost);	
	MS_STRCATS_INT(tempbuf,outbuf,",syncErr[%u]",perr1_ctt->syncErr);
	MS_STRCATS_INT(tempbuf,outbuf,",patErr[%u]",perr1_ctt->patErr);
	MS_STRCATS_INT(tempbuf,outbuf,",ccErr[%u]",perr1_ctt->ccErr);
	MS_STRCATS_INT(tempbuf,outbuf,",ccErr_av[%u]",perr1_ctt->ccErr_av);
	MS_STRCATS_INT(tempbuf,outbuf,",pmtErr[%u]",perr1_ctt->pmtErr);
	MS_STRCATS_INT(tempbuf,outbuf,",pidErr[%u]",perr1_ctt->pidErr);
	ERRLEVEL2Contxt *perr2_ctt=&stream_info.monitorInfo.errLevel2_ctt;
	MS_STRCATS_INT(tempbuf,outbuf,",TEI[%u]",perr2_ctt->TEI);
	MS_STRCATS_INT(tempbuf,outbuf,",crcErr[%u]",perr2_ctt->crcErr);
	MS_STRCATS_INT(tempbuf,outbuf,",pcrIntervalErr[%u]",perr2_ctt->pcrIntervalErr);
	MS_STRCATS_INT(tempbuf,outbuf,",pcrDIErr[%u]",perr2_ctt->pcrDIErr);
	MS_STRCATS_INT(tempbuf,outbuf,",ptsErr[%u]",perr2_ctt->ptsErr);
	ERRLEVEL3Contxt *perr3_ctt=&stream_info.monitorInfo.errLevel3_ctt;
	MS_STRCATS_INT(tempbuf,outbuf,",infoChange[%u]",perr3_ctt->infoChange);
	MS_STRCATS_INT(tempbuf,outbuf,",sdtErr[%u]",perr3_ctt->sdtErr);
	if(0==stream_info.totalnum_program){
		return (ms_buflen(outbuf)) ? ms_true : ms_false;
	}
	if(0==stream_info.totalnum_program_real){
		stream_info.totalnum_program_real=stream_info.totalnum_program;
	}
	if(stream_info.totalnum_program_real>1){
		ms_strcats(tempbuf,outbuf,",program_num[%d]",stream_info.totalnum_program_real);
		return ms_true;
	}

	for(program_index=0;program_index<stream_info.totalnum_program;program_index++){
		PROGRAMInfo *program_info=&stream_info.program_info[program_index];
		if(mpegts_api_isMpeg(stream_info.format,program_info->pmt_pid)){
			if(mssi_api_isValidPid(program_info->pmt_pid)){	
				if(program_info->period_pmt.period>0){
					ms_strcats(tempbuf,outbuf,",PMT[%#x-%s]",program_info->pmt_pid,ms_num2strU64(program_info->period_pmt.period));
				}else{
					ms_strcats(tempbuf,outbuf,",PMT[%#x]",program_info->pmt_pid);
				}
			}	
			if(mssi_api_isValidPid(program_info->pcr_pid)){
				if(program_info->period_pcr.period>0){
					ms_strcats(tempbuf,outbuf,",pcr[%#x-%s]",program_info->pcr_pid,ms_num2strU64(program_info->period_pcr.period));
				}else{
					ms_strcats(tempbuf,outbuf,",pcr[%#x]",program_info->pcr_pid);
				}
			}	

			if(ms_true==program_info->flag_hasSdt){
				ms_strcats(tempbuf,outbuf,",service");
				ms_strcat(outbuf,"[");
				if(ms_buflen(program_info->service_type)>0){
					ms_strcats(tempbuf,outbuf,"%s",program_info->service_type);
					if((ms_buflen(program_info->service_name)>0)||(ms_buflen(program_info->service_provider_name)>0)){
						ms_strcat(outbuf,",");
					}
				}
				if(ms_buflen(program_info->service_name)>0){
					ms_strcats(tempbuf,outbuf,"%s",program_info->service_name);
					if(ms_buflen(program_info->service_provider_name)>0){
						ms_strcat(outbuf,",");
					}
				}
				if(ms_buflen(program_info->service_provider_name)>0){
					ms_strcats(tempbuf,outbuf,"%s",program_info->service_provider_name);
				}
				ms_strcat(outbuf,"]");
			}
		}
		if(program_info->num_es>0){
			for(ms_s32 es_index=0;es_index<program_info->num_es;es_index++){
				ESInfo *pEsInfo=&program_info->esInfo[es_index];
				ms_strcats(tempbuf,outbuf,",%s",pEsInfo->strStreamType);
				ms_byte esCodecInfo[256]={0};
				ms_byte tbuf[256]={0};
				if(avtype_video==pEsInfo->avType){
					if(ms_buflen(pEsInfo->codecInfo.resolution)){
						ms_strcpy(esCodecInfo, pEsInfo->codecInfo.resolution);
					}else{
						ms_strcpy(esCodecInfo,"0x0");
					}
					MS_STRCATS_INT(tbuf,esCodecInfo,",%.1f fps", pEsInfo->codecInfo.float_fps);
					MS_STRCATS_STR(tbuf,esCodecInfo,",%s", pEsInfo->codecInfo.pix_fmt_string);
					MS_STRCATS_STR(tbuf,esCodecInfo,",%s", pEsInfo->codecInfo.profile_string);
					MS_STRCATS_STR(tbuf,esCodecInfo,",%s", pEsInfo->codecInfo.colorspace_string);
				}else if(avtype_audio==pEsInfo->avType){
					ms_sprintfs(esCodecInfo,"%d Hz", pEsInfo->codecInfo.sample_rate);
					MS_STRCATS_INT(tbuf,esCodecInfo,",%d bps", pEsInfo->codecInfo.bit_rate);
					MS_STRCATS_STR(tbuf,esCodecInfo,",%s", pEsInfo->codecInfo.channel_layout_string);
					MS_STRCATS_STR(tbuf,esCodecInfo,",%s", pEsInfo->codecInfo.sample_fmt_string);						
				}
				if(mpegts_api_isMpeg(stream_info.format,pEsInfo->pid)){
					if(ms_buflen(esCodecInfo)>0){
						ms_strcats(tempbuf,outbuf,"[%#x,%s]",pEsInfo->pid,esCodecInfo);
					}else{
						ms_strcats(tempbuf,outbuf,"[%#x]",pEsInfo->pid);
					}
				}else if(ms_buflen(esCodecInfo)>0){
					ms_strcats(tempbuf,outbuf,"[%s]",esCodecInfo);
				}
				if(pEsInfo->ctuInfo.flag_enable){
					MS_STRCATS_INT(tempbuf,outbuf,"-cc[error:%d]",pEsInfo->ctuInfo.numErr);
				}
			}
		}
	}
	return ms_true;
}
ms_s32 mpegts_api_buildPat( ms_byte * pTsPkt,ms_u08 *cc,ms_u08 pktSize,ms_byte adaption_field_control,
	ms_u16 transport_stream_id,PATPARAMContxt *pat_ctt)
{
	return pat_api_build(pTsPkt, cc,pktSize,adaption_field_control,transport_stream_id,pat_ctt);
}
ms_void mpegts_api_parsePat( ms_byte * bufPat,ms_s32 len)
{
	STREAMInfo stream_info;
	ms_memset(&stream_info, 0, ms_bufsize(STREAMInfo));
	MPEGTSContxt *pMpegts_ctt=ms_null;
	mpegts_init((ms_void **)&pMpegts_ctt,"parsePat", 0, &stream_info,ms_null);
	
	mpegts_parse(pMpegts_ctt, bufPat, &len);
	
	mpegts_deinit((ms_void **)&pMpegts_ctt);
}
ms_s32 mpegts_api_buildPmt( ms_byte *ptspkt,ms_u08 *cc,ms_u08 pktSize,ms_byte adaption_field_control,
	ms_u16 program_num,ms_u16 pid,ms_u16 pcr_pid,ms_s08 num_es, ESInfo *pEsInfoArry)
{
	return pmt_api_build(ptspkt, cc, pktSize, adaption_field_control,  program_num, pid, pcr_pid,num_es, pEsInfoArry);
}
static ms_s32 mpegts_innerapi_buildPmt2( ms_byte *ptspkt,ms_u08 *cc,ms_u08 pktSize,
	ms_byte adaption_field_control,PMTPARAMContxt *pmt_ctt)
{
	return pmt_api_build(ptspkt, cc, pktSize, adaption_field_control, 
		pmt_ctt->program_num, pmt_ctt->pid, pmt_ctt->pcr_pid, pmt_ctt->num_es, pmt_ctt->EsInfo);
}	
ms_void mpegts_api_parsePmt( ms_byte * bufPmt,ms_u16 pmt_pid,ms_s32 len)
{
	STREAMInfo stream_info;
	ms_memset(&stream_info, 0, ms_bufsize(STREAMInfo));
	MPEGTSContxt *pMpegts_ctt=ms_null;
	mpegts_init((ms_void **)&pMpegts_ctt,"parsePmt", 0, &stream_info,ms_null);
	
	pMpegts_ctt->paInfo.totalnum_program=1;
	pMpegts_ctt->flag_findPatInfo=ms_true;
	pMpegts_ctt->headInfo.pid=pmt_pid;
	PMTInfo *pmt_info=&pMpegts_ctt->pmtInfo[0];
	pmt_info->pid=pmt_pid;

	mpegts_parse(pMpegts_ctt, bufPmt, &len);

	mpegts_deinit((ms_void **)&pMpegts_ctt);
}
void mpegts_api_buildsdt2( ms_byte *ptspkt,ms_u08 cc,ms_u08 pktSize,ms_byte adaption_field_control,ms_u16 service_id,
	ms_string service_provider_name,ms_string service_name,ms_u16 transport_stream_id,ms_u16 original_network_id)
{
	sdt_api_build2(ptspkt,cc, pktSize, adaption_field_control, service_id,service_provider_name, service_name,
		transport_stream_id,original_network_id);
}
ms_void mpegts_api_parseSdt( ms_byte * bufSdt,ms_s32 len,ms_u16 totalnum_program)
{
	STREAMInfo stream_info;
	ms_memset(&stream_info, 0, ms_bufsize(STREAMInfo));
	MPEGTSContxt *pMpegts_ctt=ms_null;
	mpegts_init((ms_void **)&pMpegts_ctt,"parseSdt", 0, &stream_info,ms_null);

	pMpegts_ctt->paInfo.totalnum_program=totalnum_program;
	
	mpegts_parse(pMpegts_ctt, bufSdt, &len);

	mpegts_deinit((ms_void **)&pMpegts_ctt);
}

ms_void mpegts_api_buildNullPkt( ms_byte * ptspkt,ms_u08 pktSize)
{
	mssi_api_tsInit(ptspkt,SI_PID_NULL,0x00, pktSize, 0x1,0x01);
}

ms_bool mpegts_api_isEsPid(ms_u16 pid,ms_s08 num_es, ESInfo *pEsInfoArry)
{
	for(ms_s32 es_index=0;es_index<num_es;es_index++){
		ESInfo *pEsInfo=&pEsInfoArry[es_index];
		if(pid==pEsInfo->pid){
			return ms_true;
		}
	}
	ms_false;
}
ms_s32 mpegts_api_getTsPos(ms_string inurl)
{
/*获取协议相关信息，目前仅支持文件*/
	if(ms_true!=msptc_api_matchByUrlAndIndex(inurl,msptc_file)){
		ms_errRet(-1, "No need to getTsPos:%s", inurl);
	}
/*获取协议句柄,并读取数据*/		
	URLProtocol *msfile_ptl=msptc_api_matchByPtcindex(msptc_file);
	URLContext in_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url,inurl);
	in_ctt.flags=FLAG_READ|FLAG_FILE_NOCREATE;
	if(msfile_ptl->url_open(&in_ctt)<0){
		ms_errRet(-2, "url_open %s failed", in_ctt.url);
	}
	//读取足够多的数据，然后定位MPEGTS头，最后丢掉非MPEGTS数据
	ms_byte databuf[5*ms_KB]={0};
	ms_s32 len=msfile_ptl->url_read(&in_ctt,databuf,ms_bufsize(databuf));
	msfile_ptl->url_close(&in_ctt);
/*解析数据，获取包大小*/			
	if(len>0){
		return mssi_api_getTsOffset(databuf, len);
	}else{
		ms_errRet(-3, "url_read %s failed(len:%d)", in_ctt.url,len);
	}
}
ms_s32 mpegts_api_getTsPos2(ms_byte *buf,ms_s32 len)
{	
	return mssi_api_getTsOffset(buf, len);
}
ms_u08 mpegts_api_getTsPktSize(ms_string inurl,ms_string interfaceIn,char *format,ms_bool flag_vod)
{
/*获取协议相关信息*/
	URLContext in_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url, inurl);
	msptc_api_urlSplit_urlctt( &in_ctt);
/*获取协议句柄,并读取数据*/	
	ENUPROTOCOLIndex   ptcIndex=msptc_api_getPtcindex(inurl,format,flag_vod);
	if(msptc_file==ptcIndex){
		in_ctt.flags=FLAG_READ|FLAG_FILE_NOCREATE;
	}else{
		in_ctt.flags=FLAG_READ;
		if(msptc_udp==ptcIndex||msptc_rtp==ptcIndex){
			ms_bool is_multicast = msnet_api_isMulticast(in_ctt.url_info.hostname);
			ms_debug("is_multicast:%d,hostname:%s,interfaceIn:%s", is_multicast,in_ctt.url_info.hostname,interfaceIn);
			if(is_multicast && ms_null!=interfaceIn&&ms_buflen(interfaceIn)>0 ){
				ms_funcLine;
				in_ctt.opt.udp.ifcIn=interfaceIn;
			}
		}
	}	
	URLProtocol *msfile_ptl=msptc_api_matchByName(in_ctt.url_info.ptcName);
	if(!msfile_ptl){
		ms_errRet(0, "No find msfile_ptl(ptcName:%s)", in_ctt.url_info.ptcName);
	}
	if(msfile_ptl->url_open(&in_ctt)<0){
		ms_errRet(0, "url_open %s failed", in_ctt.url);
	}
	//读取足够多的数据，然后定位MPEGTS头，最后丢掉非MPEGTS数据
	ms_byte databuf[5*ms_KB]={0};
	ms_s32 len=msfile_ptl->url_read(&in_ctt,databuf,ms_bufsize(databuf));
	msfile_ptl->url_close(&in_ctt);
/*解析数据，获取包大小*/		
	if(len>0){
		ms_s32 pos=mssi_api_getTsOffset(databuf, len);
		return mssi_api_getPktSize(&databuf[pos], (len-pos));
	}else{
		ms_errRet(0, "url_read %s failed(len:%d)", in_ctt.url,len);
	}
}
ms_u08 mpegts_api_getTsPktSize2(ms_string inurl,ms_string interfaceIn,char *format,ms_bool flag_vod)
{
	ms_u08 pktSize=mpegts_api_getTsPktSize(inurl, interfaceIn,format, flag_vod);
	if(0==pktSize){
		pktSize=TS_SIZE_BASE;
	}
	return pktSize;
}
ms_u08 mpegts_api_getTsPktSize3(ms_byte *databuf,ms_s32 len)
{
/*解析数据，获取包大小*/		
	ms_s32 pos=mssi_api_getTsOffset(databuf, len);
	return mssi_api_getPktSize(&databuf[pos], (len-pos));
}
ms_bool mpegts_api_isFileNMpegts(ENUPROTOCOLIndex ptcIndex,ms_string purl,ms_string format)
{
	if(msptc_file!=ptcIndex){
		return ms_false;
	}
	if((ms_null!=purl& msstr_api_isCasestr(purl, ".ts"))
		||(ms_null!=format& ms_strncmp_saeq(format, AVFORMAT_MPEGTS))){
	//TS文件
		return ms_false;
	}else{
	//非TS文件
		return ms_true;
	}
}
ms_bool mpegts_api_isMpeg(ms_string format,ms_u16 pid)
{
	if( pid>0){
		return ms_true;
	}else{
		if(ms_null!=format){
			if(ms_strncmp_saeq(format, AVFORMAT_MPEGTS)
				||ms_strncmp_saeq(format, AVFORMAT_HLS)
				||ms_strncmp_saeq(format, AVFORMAT_RTP_MPEGTS)){
				return ms_true;
			}
		}
	}
	
	return ms_false;
}
ms_bool mpegts_api_isMpeg2(ms_string format)
{
	if(ms_null==format){
		return ms_false;
	}
	if( mpegts_api_isMpeg(format,0)||ms_strncmp_saeq(format,AVFORMAT_RTP)){
		return ms_true;
	}else{
		return ms_false;
	}

}
//功能：将RTP文件提取为MPEGTS文件
void mpegts_api_rtpDemux(ms_u32 logopt,ms_string inurl, ms_string outurl,ms_s32 lenRtpPkt,ms_bool flag_rtpOverTcp)
{
	mslog_api_init( logopt, PATH_LOG_MSCORE,"rtpDemux.txt", ms_null);
	msmem_api_init(ms_false);
	msptc_api_init();
	msptc_api_register();
	URLProtocol *msfile_ptl=msptc_api_matchByPtcindex(msptc_file);
	URLProtocol *msrtp_ptl=msptc_api_matchByPtcindex(msptc_rtp);
	URLProtocol *msudp_ptl=msptc_api_matchByPtcindex(msptc_udp);

	//读取RTP文件-->将数据按UDP发送-->按RTP读取（去掉RTP头）-->进行文件保存

	URLContext in_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url,inurl);
	msfile_ptl->url_open(&in_ctt);
	
	URLContext inTemUdp_ctt;
	ms_memset(&inTemUdp_ctt,0,sizeof(URLContext));
	ms_strcpy(inTemUdp_ctt.url,"udp://127.0.0.1:2001");
	inTemUdp_ctt.flags|=FLAG_WRITE;
	msudp_ptl->url_open(&inTemUdp_ctt);
	
	URLContext outTemRtp_ctt;
	ms_memset(&outTemRtp_ctt,0,sizeof(URLContext));
	ms_strcpy(outTemRtp_ctt.url,"rtp://127.0.0.1:2001");
	msrtp_ptl->url_open(&outTemRtp_ctt);

	URLContext out_ctt;
	ms_memset(&out_ctt,0,sizeof(URLContext));
	ms_strcpy(out_ctt.url,outurl);
	out_ctt.flags|=FLAG_WRITE;
	msfile_ptl->url_open(&out_ctt);


	ms_byte rtpBuf[2048]={0};
	ms_byte databuf[2048]={0};

	STREAMInfo stream_info;
	ms_memset(&stream_info, 0, ms_bufsize(STREAMInfo));
	MPEGTSContxt *pMpegts_ctt=ms_null;
	mpegts_init((ms_void **)&pMpegts_ctt,"rtpDemux", mode_monitor, &stream_info,ms_null);
	while(1){	
ms_reread:		
		if(msfile_ptl->url_read(&in_ctt,rtpBuf,lenRtpPkt)!=lenRtpPkt){
			break;
		}
		if(flag_rtpOverTcp){
			IFRAMEContext rtspif_ctt;
			RTSPDATAType ret=msptc_api_rtpIframeGet(&rtspif_ctt,rtpBuf,4);
			if(RTSP_DATATYPE_RTP==ret
				||RTSP_DATATYPE_RTCP==ret){
				msudp_ptl->url_write(&inTemUdp_ctt,&rtpBuf[4],(lenRtpPkt-4));
			}else{
				ms_errGoto(ms_reread,"msptc_api_rtpIframeGet  is failed(error_no=%d)",ret);
			}
		}else{
			msudp_ptl->url_write(&inTemUdp_ctt,rtpBuf,lenRtpPkt);
		}
		
		int len=msrtp_ptl->url_read(&outTemRtp_ctt,databuf,2048);
		//ms_debug("--------------------------------b");
		mpegts_parse(pMpegts_ctt, databuf, &len);
		//ms_debug("--------------------------------e");
		msfile_ptl->url_write(&out_ctt, databuf, len);
	}
	mpegts_deinit((ms_void **)&pMpegts_ctt);
	msfile_ptl->url_close(&out_ctt);
	msudp_ptl->url_close(&inTemUdp_ctt);
	msrtp_ptl->url_close(&outTemRtp_ctt);
	msfile_ptl->url_close(&in_ctt);

	msptc_api_deinit();
	msmem_api_deinit();
	mslog_api_deinit();
}

//功能：将格式为“|非MPEGTS包|MPEGTS包|MPEGTS包|”的MPEGTS文件，提取成格式为“|MPEGTS包|MPEGTS包|”的MPEGTS文件
void mpegts_api_tsExtract(ms_u32 logopt,ms_string inurl, ms_string outurl)
{
	DBG_CC	=ms_true;
	mslog_api_init( logopt, PATH_LOG_MSCORE,"tsExtract.txt", ms_null);
	msmem_api_init(ms_false);
	msptc_api_init();
	msptc_api_register();
	mpegts_api_init();
	
/*获取包大小*/
	ms_u08 pktSize=mpegts_api_getTsPktSize2(inurl,ms_null,ms_null,ms_false);
/*获取文件句柄*/
	URLProtocol *msfile_ptl=msptc_api_matchByPtcindex(msptc_file);
/*打开输入文件*/	
	URLContext in_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url,inurl);
	in_ctt.flags=FLAG_READ|FLAG_FILE_NOCREATE;
	//找到TS的同步头位置
	in_ctt.opt.file.pos=mpegts_api_getTsPos(inurl);
	ms_info("pos:%d", in_ctt.opt.file.pos);
	msfile_ptl->url_open(&in_ctt);
/*打开输出文件*/	
	URLContext out_ctt;
	ms_strcpy(out_ctt.url,outurl);
	out_ctt.flags|=FLAG_WRITE;
	msfile_ptl->url_open(&out_ctt);
/*进行数据提取*/	
	ms_byte databuf[TS_PACKET_BESTLEN_CUS(pktSize)];
	ms_s32 len=0;

	STREAMInfo stream_info;
	ms_memset(&stream_info, 0, ms_bufsize(STREAMInfo));	
	MPEGTSContxt *pMpegts_ctt=ms_null;
	mpegts_init((ms_void **)&pMpegts_ctt,"te", mode_monitor, &stream_info,ms_null);
	while(1){	
		len=msfile_ptl->url_read(&in_ctt,databuf,TS_PACKET_BESTLEN_CUS(pktSize));
		if(len!=TS_PACKET_BESTLEN_CUS(pktSize)){
			break;
		}
		//ms_debug("--------------------------------b");
		mpegts_parse(pMpegts_ctt, databuf, &len);
		//ms_debug("--------------------------------e");
		msfile_ptl->url_write(&out_ctt, databuf, len);
	}
	msfile_ptl->url_close(&out_ctt);
	msfile_ptl->url_close(&in_ctt);

	mpegts_deinit((ms_void **)&pMpegts_ctt);
	msptc_api_deinit();
	msmem_api_deinit();
	mslog_api_deinit();
}
//功能：TS流监测
void mpegts_innerapi_tsMonitor(ms_u32 logopt,ms_string logfile,ms_string inurl,ms_string interfaceIn,
	ms_bool flag_vod,ms_u32 mode,ms_bool flag_showStatistics)
{
	mslog_api_init( logopt, PATH_LOG_MSCORE,logfile, ms_null);
	msmem_api_init(ms_false);
	msptc_api_init();
	msptc_api_register();
	mpegts_api_init();
	
	mssignal_api_catch("tsMonitor");
/*获取包大小*/	
	ms_u08 pktSize=mpegts_api_getTsPktSize2(inurl,interfaceIn,ms_null, ms_false);
/*休眠1秒，避免403问题*/
	ms_sleep(2);
/*打开输入流*/		
	URLContext in_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url,inurl);
	msptc_api_urlSplit_urlctt(&in_ctt);
	ENUPROTOCOLIndex ptcIndex=msptc_api_getPtcindex(in_ctt.url,ms_null,flag_vod);
	URLProtocol *msptl_in=msptc_api_matchByName(in_ctt.url_info.ptcName);
	if(!msptl_in){
		ms_errNoret("No find msptl_in(ptcName:%s)", in_ctt.url_info.ptcName);
	}
	if(msptc_file==ptcIndex){
		in_ctt.flags=FLAG_READ|FLAG_FILE_NOCREATE;
	}else{
		in_ctt.flags=FLAG_READ;
	}
	ms_byte localhostip[256]={0};
	//组播输入，则设置输入网卡和输入IP地址
	if(msptc_udp==ptcIndex||msptc_rtp==ptcIndex){
		ms_bool is_multicast = msnet_api_isMulticast(in_ctt.url_info.hostname);
		if(is_multicast && ms_null!=interfaceIn&&ms_buflen(interfaceIn)>0 ){
			in_ctt.opt.udp.ifcIn=interfaceIn;
		}
	}
	if(msptl_in->url_open(&in_ctt)<0){
		ms_errNoret("url_open %s failed",inurl);
	}
/*进行数据监测*/
	ms_byte databuf[TS_PACKET_BESTLEN_CUS(pktSize)];
	ms_s32 len=0;
	ms_s32 time=1;

	STREAMInfo stream_info;
	ms_memset(&stream_info, 0, ms_bufsize(STREAMInfo));	
	MPEGTSContxt *pMpegts_ctt=ms_null;
	mpegts_init((ms_void **)&pMpegts_ctt,"tm", mode, &stream_info,ms_null);
	
	ms_u64 Etimep=mstime_api_upSec();
	while(1){	
		len=msptl_in->url_read(&in_ctt,databuf,TS_PACKET_BESTLEN_CUS(pktSize));
		if(len>0){
			mpegts_update(pMpegts_ctt);
			mpegts_parse(pMpegts_ctt, databuf, &len);
			time=1;
		}else if(time++>100){
			break;
		}
		if(flag_showStatistics && mstime_api_counterAsyncSec_Up(&Etimep, ms_seconds(5))){
			mpegts_innerapi_showInfo(&stream_info);
		}
	}
	msptl_in->url_close(&in_ctt);
/*打印监测统计结果*/	
	if(flag_showStatistics){
		mpegts_innerapi_showInfo(&stream_info);
	}
	mpegts_deinit((ms_void **)&pMpegts_ctt);
	msptc_api_deinit();
	msmem_api_deinit();
	mslog_api_deinit();
}
//功能：TS流的CC检测，支持音视频、PAT、PMT和SDT表	
void mpegts_api_tsMonitor(ms_u32 logopt,ms_string inurl,ms_string interfaceIn,
	ms_bool dbgAv,ms_bool dbgEs,ms_bool flag_vod)
{
	ms_u32 mode=mode_monitor|mode_pidStatistics;
	ms_info("dbgAv,dbgEs,flag_vod:%u,%u,%u", dbgAv,dbgEs,flag_vod);
	if(dbgAv){
		DBG_AV	=ms_true;
	}
	if(dbgEs){
		DBG_PCR =ms_true;
		DBG_PES_PTSDTS	=ms_true;
		mode|=mode_parsePes;
	}	
	//DBG_CC	=ms_true;
	mpegts_innerapi_tsMonitor(logopt, "tsMonitor.txt", inurl,interfaceIn, flag_vod,  mode, ms_true);
}

//PAT、PMT和SDT表的构建和解析
void mpegts_api_tsApiTest(ms_u32 logopt,ms_string target)
{
#define TEST_PAT	(1<<0)
#define TEST_PMT	(1<<1)
#define TEST_SDT	(1<<2)

	mslog_api_init( logopt, PATH_LOG_MSCORE,"tsApiTest.txt", ms_null);
	msmem_api_init(ms_false);
	mssignal_api_catch("tsApiTest");
	
	mpegts_api_init();
	DBG_PCR	=ms_true;
	ms_byte bufPat[5120]={0};
	ms_byte bufPmt[1024]={0};
	ms_byte bufSdt[5120]={0};
	ms_s08 num_es=2;
	ESInfo esInfo[2];
	ms_u08 cc=0X0;
	ms_memset(esInfo, 0, (ms_bufsize(ESInfo)*2));
	esInfo[0].pid=0x101;
	esInfo[0].streamType=streamtype_video_mpeg4;
	esInfo[1].pid=0x102;
	esInfo[1].streamType=streamtype_audio_aac_latm;

	ms_u32 target_bit=0;	
	if(msstr_api_isCasestr(target, "ALL")||msstr_api_isCasestr(target, "all")){
		target_bit|=TEST_PAT;
		target_bit|=TEST_PMT;
		target_bit|=TEST_SDT;
	}else{
		if(msstr_api_isCasestr(target, "PAT")||msstr_api_isCasestr(target, "pat")){
			target_bit|=TEST_PAT;
		}
		if(msstr_api_isCasestr(target, "PMT")||msstr_api_isCasestr(target, "pmt")){
			target_bit|=TEST_PMT;
		}
		if(msstr_api_isCasestr(target, "SDT")||msstr_api_isCasestr(target, "sdt")){
			target_bit|=TEST_SDT;
		}	
	}
if(target_bit&TEST_PAT){	
ms_info("PAT表的构建和解析测试1");
	PATPARAMContxt pat_ctt;
	ms_memset(&pat_ctt, 0, ms_bufsize(PATPARAMContxt));	
	pat_ctt.totalnum=MAX_PROGRAM;
	for(ms_u32 index=0;index<pat_ctt.totalnum;index++){
		pat_ctt.serviceidList[index]=200+index;
		pat_ctt.pmtIdList[index]=300+index;
	}	
	ms_s32 lenPat=mpegts_api_buildPat(bufPat, &cc,TS_SIZE_BASE,0x03,TS_DEFAULT_TRANSPORT_STREAM_ID,&pat_ctt);		
	ms_debug("lenPat:%d",lenPat);
	mpegts_api_parsePat(bufPat,lenPat);
ms_info("PAT表的构建和解析测试2"); 
	pat_ctt.totalnum=1;
	mpegts_api_buildPat(bufPat,&cc, TS_SIZE_BASE,0x01,TS_DEFAULT_TRANSPORT_STREAM_ID,&pat_ctt);
	mpegts_api_parsePat(bufPat,TS_SIZE_BASE);
}
if(target_bit&TEST_PMT){
ms_info("PMT表的构建和解析测试1");	
	mpegts_api_buildPmt(bufPmt, &cc, TS_SIZE_BASE, 0x01,TS_DEFAULT_SERVICEID, TS_DEFAULT_PMTPID,0x100, num_es, esInfo);
	mpegts_api_parsePmt(bufPmt, TS_DEFAULT_PMTPID, TS_SIZE_BASE);
ms_info("PMT表的构建和解析测试2"); 
	mpegts_api_buildPmt(bufPmt, &cc, TS_SIZE_BASE, 0x03,TS_DEFAULT_SERVICEID, TS_DEFAULT_PMTPID,0x100, num_es, esInfo);
	mpegts_api_parsePmt(bufPmt, TS_DEFAULT_PMTPID, TS_SIZE_BASE);
}
if(target_bit&TEST_SDT){
ms_info("SDT表的构建和解析测试1");	
	mpegts_api_buildsdt2(bufSdt,0x00,TS_SIZE_BASE, 0x01,TS_DEFAULT_SERVICEID, 
		"湖南广电网络", "湖南卫视",TS_DEFAULT_TRANSPORT_STREAM_ID,TS_DEFAULT_ORIGINAL_NETWORK_ID );
	mpegts_api_parseSdt( bufSdt ,TS_SIZE_BASE,1);
ms_info("SDT表的构建和解析测试2");
	mpegts_api_buildsdt2(bufSdt,0x00,TS_SIZE_BASE, 0x03, TS_DEFAULT_SERVICEID,
		"湖南广电网络", "湖南卫视" ,TS_DEFAULT_TRANSPORT_STREAM_ID,TS_DEFAULT_ORIGINAL_NETWORK_ID);
	mpegts_api_parseSdt( bufSdt ,TS_SIZE_BASE,1);
	ms_info("SDT表的构建和解析测试3");
	SDTPARAMContxt sdt_ctt;
	ms_memset(&sdt_ctt, 0, ms_bufsize(SDTPARAMContxt));
	sdt_ctt.totalnum=MAX_PROGRAM;
	for(ms_u16 index=0;index<sdt_ctt.totalnum;index++){
		sdt_ctt.serviceidList[index]=(200+index);
		sdt_ctt.service_type[index]=servicetype_digital_radio;
		ms_sprintfs(sdt_ctt.spnList[index],"湖南广电网络");
		ms_sprintfs(sdt_ctt.snList[index],"湖南卫视%03d",index);
	}
	ms_u08 cc_sdt=0;
	ms_u32 len=sdt_api_build(bufSdt,&cc_sdt,TS_SIZE_BASE, 0x03,TS_DEFAULT_TRANSPORT_STREAM_ID,
		TS_DEFAULT_ORIGINAL_NETWORK_ID,&sdt_ctt);
	ms_debug("len:%d", len);
	mpegts_api_parseSdt( bufSdt ,len,sdt_ctt.totalnum);
}
	msmem_api_deinit();
	mslog_api_deinit();
}
ms_void mpegts_api_init(ms_void)
{
	if(ms_null==str_streamType[0]){
		for(ms_u32 streamtype_index=0;streamtype_index<streamtype_max;streamtype_index++){
			str_streamType[streamtype_index]=UNKNOW_TYPE;
		}
		str_streamType[streamtype_video_mpeg1]		="mpeg1v";
		str_streamType[streamtype_video_mpeg2]		="mpeg2v";
		str_streamType[streamtype_audio_mpeg1]		="mpeg1a";
		str_streamType[streamtype_audio_mpeg2]		="mpeg2a";
		str_streamType[streamtype_private_sections]	="private_sections";
		str_streamType[streamtype_pes_private_data]	="pes_private_data";
		str_streamType[streamtype_ns_mheg]			="MHEG";
		str_streamType[streamtype_ns_AnnexA_DSM_CC]	="Annex A -DSM CC";
		str_streamType[streamtype_ns_h2221]			="H.222.1";
		str_streamType[streamtype_ns_typeA]			="type A";
		str_streamType[streamtype_ns_typeB]			="type B";
		str_streamType[streamtype_ns_typeC]			="type C";
		str_streamType[streamtype_ns_typeD]			="type D";
		str_streamType[streamtype_ns_auxiliary]		="auxiliary";
		str_streamType[streamtype_audio_aac]		="aac";
		str_streamType[streamtype_video_mpeg4]		="mpeg4";
		str_streamType[streamtype_audio_aac_latm]	="aac_latm";
		str_streamType[streamtype_ns_FlexMux_pes]	="FlexMux_pes";
		str_streamType[streamtype_ns_FlexMux_14496sec]="FlexMux_14496sec";
		str_streamType[streamtype_ns_sync_download]	="sync_download";
		str_streamType[streamtype_metadata]			="metadata";
		str_streamType[streamtype_ns_metadata_sections]="metadata_sections";
		str_streamType[streamtype_ns_metadata_138186DataCarousel]="138186DataCarousel";
		str_streamType[streamtype_ns_metadata_138186ObjectCarousel]="138186ObjectCarousel";
		str_streamType[streamtype_ns_metadata_138186sync_download]="138186sync_download";
		str_streamType[streamtype_ns_mpeg2_ipmp]="mpeg2_ipmp";
		str_streamType[streamtype_video_h264]		="h264";
		str_streamType[streamtype_video_hevc]		="hevc";	
		str_streamType[streamtype_video_cavs]		="cavs";
		str_streamType[streamtype_ns_ipmp]		="ipmp";	
		str_streamType[streamtype_audio_ac3]		="ac3";		
		str_streamType[streamtype_audio_dts]		="dts";
		str_streamType[streamtype_audio_truehd] 	="truehd";
		str_streamType[streamtype_ns_sit]		="sit";
		str_streamType[streamtype_audio_eac3]		="eac3";
		str_streamType[streamtype_subtitle_pgs] 	="PGS";
		str_streamType[streamtype_subtitle_igs]		="IGS";
		str_streamType[streamtype_subtitle_text]	="text";
		str_streamType[streamtype_video_dirac]		="dirac";
		str_streamType[streamtype_video_vc1]		="vc1";		
	}
	if(ms_null==str_serviceType[0]){
		for(ms_u32 servicetype_index=0;servicetype_index<servicetype_max;servicetype_index++){
			str_serviceType[servicetype_index]=UNKNOW_TYPE;
		}
		str_serviceType[servicetype_digital_tv]						="digital_tv";
		str_serviceType[servicetype_digital_radio]					="digital_radio";
		str_serviceType[servicetype_digital_teletext]				="teletext";
		str_serviceType[servicetype_advanced_codec_digital_radio]	="advanced_codec_digital_radio";
		str_serviceType[servicetype_mpeg2_digital_hdtv]				="mpeg2_digital_hdtv";
		str_serviceType[servicetype_advanced_codec_digital_sdtv]	="advanced_codec_digital_sdtv";
		str_serviceType[servicetype_advanced_codec_digital_hdtv]	="advanced_codec_digital_hdtv";
	}

	if(ms_null==str_avType[0]){
		for(ms_u32 av_index=0;av_index<avtype_max;av_index++){
			str_avType[av_index]=UNKNOW_TYPE;
		}
		str_avType[avtype_video]	="video";
		str_avType[avtype_audio]	="audio";
		str_avType[avtype_data]		="data";
		str_avType[avtype_subtitle]	="subtitle";
		str_avType[avtype_attachment]="attachment";
		str_avType[avtype_nb]		="nb";
	}
}
ms_string mpegts_api_getStreamType(ms_u08 stream_type)
{
	if(((ms_null==str_streamType[0]))||stream_type>streamtype_max){
		return UNKNOW_TYPE;	
	}else{
		return str_streamType[stream_type];	
	}
}
ms_string mpegts_api_getServiceType(ms_u16 service_type)
{
	if(((ms_null==str_serviceType[0]))||service_type>servicetype_max){
		return UNKNOW_TYPE;	
	}else{
		return str_serviceType[service_type];	
	}
}
ms_string mpegts_api_getAVType(ms_u08 avType)
{
	if(((ms_null==str_avType[0]))||avType>avtype_max){
		return UNKNOW_TYPE;	
	}else{
		return str_avType[avType];	
	}
}

static ms_void mpegts_init(ms_void ** param,ms_string tag,ms_u32 mode,void *stream_info, void *mapList_ctt)
{
	if(!(*param)){
		ms_mallocDes_noRet((*param),sizeof(MPEGTSContxt),"MPEGTSContxt","resolve-mpegts");
		ms_memset((*param), 0, sizeof(MPEGTSContxt));
		MPEGTSContxt *rlmpegts=(MPEGTSContxt *)(*param);
		ms_strcpy( rlmpegts->tag,tag);
		rlmpegts->mode=mode;
		rlmpegts->stream_info=stream_info;
		rlmpegts->mapList_ctt=mapList_ctt;
	}else{
		MPEGTSContxt *rlmpegts=(MPEGTSContxt *)(*param);
	/*备份原有初始化数据*/		
		ms_byte tag[256]={0};
		ms_strcpy( tag,rlmpegts->tag);
		ms_u32 mode=rlmpegts->mode;
		STREAMInfo *stream_info=rlmpegts->stream_info;
		MAPLISTContxt * mapList_ctt=rlmpegts->mapList_ctt;
		MONITORInfo monitorInfo;
		ms_memcpy(&monitorInfo, &stream_info->monitorInfo, ms_bufsize(MONITORInfo));
	/*格式化数据*/	
		ms_memset(rlmpegts, 0, ms_bufsize(MPEGTSContxt));
		ms_memset(stream_info, 0, ms_bufsize(STREAMInfo));	
	/*初始化rlmpegts*/	
		ms_strcpy( rlmpegts->tag,tag);
		rlmpegts->mode=mode;
		rlmpegts->stream_info=stream_info;
		rlmpegts->mapList_ctt=mapList_ctt;
		ms_memcpy(&stream_info->monitorInfo, &monitorInfo, ms_bufsize(MONITORInfo));
	}
	MPEGTSContxt *rlmpegts=(MPEGTSContxt *)(*param);
	mpegts_innerapi_init2(rlmpegts);
}
static ms_s08 mpegts_parse(ms_void *param,ms_byte *buf, ms_s32 *len)
{
	ms_bufcheckRet(-1, param);
	MPEGTSContxt *rlmpegts=(MPEGTSContxt *)param;
/*如果是信息解析模式且已获取到所有信息，则不再继续解析，避免消耗*/
	if((mode_parseInfo&rlmpegts->mode) &&(ms_true==rlmpegts->flag_findAllInfo)){
		return 0;
	}
	
	MPEGTSPKTType tsPktType[512]={pktType_unknow};
	
/*获取TS包大小*/	
	ms_s32 len_org=(*len);
	if(0==rlmpegts->pktSize){
		rlmpegts->pktSize=mpegts_api_getTsPktSize3(buf, len_org);
		if(0==rlmpegts->stream_info->pktSize){
			rlmpegts->stream_info->pktSize=rlmpegts->pktSize;
		}
	}
/*解析TS包*/	
	ms_u32 numTs=(len_org/rlmpegts->pktSize);
	for(ms_u32 pkt_index=0;pkt_index<numTs;pkt_index++){
		ms_enDbg(DBG_COMMON,"======================pkt(%u),len(%d)======================",rlmpegts->pktSize,len_org);
		ms_byte *tspkt=&buf[rlmpegts->pktSize*pkt_index];

	/*TS同步头判断*/
		if (!mssi_api_isTs_byPktSize(tspkt, (len_org-rlmpegts->pktSize*pkt_index), rlmpegts->pktSize)){
			mpegts_innerapi_syncLost(rlmpegts,len_org,pkt_index);
			return -1;
		}
		
	/*解析TS的包头信息*/
		mssi_api_headerParse( &rlmpegts->headInfo, tspkt,rlmpegts->pktSize,DBG_TSHEAD_MODE);
		
	/*再次通过调整字段进行TS包判断*/
		if(!mssi_api_isTs_byAdaptation(rlmpegts->headInfo,tspkt,DBG_SYNC,rlmpegts->tag)){
			mpegts_innerapi_syncLost(rlmpegts,len_org,pkt_index);
			return -1;
		}
		
	/*统计PID*/
		if(mode_pidStatistics&rlmpegts->mode){
			mpegts_innerapi_pidStatistics(rlmpegts);
		}
		
	/*传输错误*/	
		if(rlmpegts->headInfo.transporterror_indicator){
			rlmpegts->stream_info->monitorInfo.errLevel2_ctt.TEI+=1;
		}
		
	/*跳过空包*/
		if(mpegts_innerapi_filterNullPkt(rlmpegts)){
			tsPktType[pkt_index]=pktType_null;
			continue;
		}
		
	/*无效数据，无需再继续解析*/	
		if(0==rlmpegts->headInfo.adaptation_field_ctr){
			tsPktType[pkt_index]=pktType_unknow;
			continue;
		}
		
	/*过滤PAT表：获取PMT表信息和节目总数，更新发送间隔*/
		if(mpegts_innerapi_filterPat(rlmpegts,tspkt)){
			tsPktType[pkt_index]=pktType_pat;
			continue;
		}
		
	/*过滤SDT表：获取节目类型、名称和运营商，更新发送间隔*/
		if(mpegts_innerapi_filterSdt(rlmpegts,tspkt)){
			tsPktType[pkt_index]=pktType_sdt;
			continue;
		}
		
	/*过滤PMT表：获取具体节目的信息，包括节目ID、音视频PID、PCR的PID等，更新发送间隔*/	
		if(mpegts_innerapi_filterPmt(rlmpegts,tspkt)){
			tsPktType[pkt_index]=pktType_pmt;
			if(ms_false==rlmpegts->flag_findAllInfo){
				rlmpegts->flag_findAllInfo=mpegts_innerapi_isGetAllInfo(rlmpegts);
			}
			continue;
		}
		
	/*获取全部信息前，不进行ES和PCR的处理*/		
		if(!rlmpegts->flag_findAllInfo){
			continue;
		}
		
	/*获取可用于控制码率的ES：使用视频ES*/
		if(ms_null==rlmpegts->pEsInfo_btcPtsDts){
			rlmpegts->pEsInfo_btcPtsDts=mpegts_innerapi_getBtcEs(rlmpegts);
		}

	/*过滤PCR：进行码率控制，更新发送间隔*/
		ms_bool flag_pcr=mpegts_innerapi_filterPcr(rlmpegts,tspkt);
		if(flag_pcr){
			tsPktType[pkt_index]=pktType_pcr;
		}
		
	/*过滤ES数据：更新发送间隔*/
		if(mpegts_innerapi_filterEs(rlmpegts,tspkt,flag_pcr,0)&&(!flag_pcr)){
			tsPktType[pkt_index]=pktType_es;
		}					
	}
/*过滤掉数据包*/
	ms_s32 out_index=0;
	(*len)=0;
	for(ms_s32 pkt_index=0;pkt_index<numTs;pkt_index++){
		if(pktType_null==tsPktType[pkt_index]&&mode_filterNullPkt&rlmpegts->mode){
			continue;
		}
		if(pktType_sdt==tsPktType[pkt_index]&&rlmpegts->stream_info->flag_rebuild_sdt){
			continue;
		}
		if(pktType_pat==tsPktType[pkt_index]&&rlmpegts->stream_info->flag_rebuild_pat){
			continue;
		}
		if(pktType_pmt==tsPktType[pkt_index]&&rlmpegts->stream_info->flag_rebuild_pmt){
			continue;
		}		
		if(out_index!=pkt_index){
			ms_memcpy(&buf[out_index*rlmpegts->pktSize], &buf[pkt_index*rlmpegts->pktSize], rlmpegts->pktSize);
		}
		(*len)+= rlmpegts->pktSize;
		out_index+=1;
	}
	return 0;
}
static ms_s32 mpegts_update(ms_void *param)
{
	ms_bufcheckRet(-1,param);
	MPEGTSContxt *rlmpegts=(MPEGTSContxt *)param;	
	STREAMInfo *stream_info=rlmpegts->stream_info;	
	//ms_debug("flag_findAllInfo,flag_update:%d,%d",rlmpegts->flag_findAllInfo ,rlmpegts->flag_update );
	if(rlmpegts->flag_reparse){
		ms_waring("清除原有节目信息数据，并重新进行节目解析");
		mpegts_init((void **)&rlmpegts,ms_null,0,ms_null,ms_null);
		return -1;
	}
/*未查找完所有信息前，不进行节目信息更新*/	
	if(ms_false==rlmpegts->flag_findAllInfo){
		return -1;
	}
/*防止频繁操作*/		
	if(ms_false==rlmpegts->flag_update){
		return -1;
	}
	ms_strcpy(rlmpegts->stream_info->format,AVFORMAT_MPEGTS);
	rlmpegts->flag_update=ms_false;
/*更新总节目数量*/		
	stream_info->totalnum_program=rlmpegts->paInfo.totalnum_program;
	stream_info->totalnum_program_real=rlmpegts->paInfo.totalnum_program_real;
/*更新节目数据*/	
	for(ms_u08 program_index=0;program_index<stream_info->totalnum_program;program_index++){
		PMTInfo *pmt_info=&rlmpegts->pmtInfo[program_index];
		PROGRAMInfo *program_info=&stream_info->program_info[program_index];
		program_info->pmt_pid=pmt_info->pid;
		program_info->program_num=pmt_info->program_num;
		program_info->pcr_pid=pmt_info->tableInfo.pid_pcr;
	#if 0	
		if(mpegts_innerapi_getSharPidNum_byPcrPid(rlmpegts, program_info->pcr_pid)>1){
			program_info->sharePid|=PCR_PIDSHARE_PROGRAM;
		}
	#else
		PMTINFOList mtInfo_list;
		mpegts_innerapi_findPmt_byPcrPid(rlmpegts,program_info->pcr_pid,&mtInfo_list);
		if(mtInfo_list.num>1){
			program_info->sharePid|=PCR_PIDSHARE_PROGRAM;
			program_info->sharePid_num=mtInfo_list.num;
			for(ms_u32 index=0;index<mtInfo_list.num;index++){
				program_info->sharePid_program_num[index]=mtInfo_list.pmtInfo[index]->program_num;
			}
		}
	#endif
		program_info->num_es=pmt_info->tableInfo.num_es;
		for(ms_s32 es_index=0;es_index<program_info->num_es;es_index++){
			ESInfo *pPEsInfo=&program_info->esInfo[es_index];
			ESInfo *pMEsInfo=&pmt_info->tableInfo.esInfo[es_index];
			pPEsInfo->pid=pMEsInfo->pid;
			pPEsInfo->streamType=pMEsInfo->streamType;
			pPEsInfo->len_esInfo=pMEsInfo->len_esInfo;
			ms_memcpy(pPEsInfo->esInfo, pMEsInfo->esInfo, pMEsInfo->len_esInfo);
			pPEsInfo->avType=pMEsInfo->avType;
			ms_strcpy(pPEsInfo->strStreamType, pMEsInfo->strStreamType);
			if(program_info->pcr_pid==pPEsInfo->pid){
				if(avtype_video==pMEsInfo->avType){
					program_info->sharePid|=PCR_PIDSHARE_VIDEO;
				}else if(avtype_audio==pMEsInfo->avType){
					program_info->sharePid|=PCR_PIDSHARE_AUDIO;
				}else{
					program_info->sharePid|=PCR_PIDSHARE_OTHERES;
				}
			}
		}	
		if(pmt_info->sdt_info){
			ms_strcpy(program_info->service_type,mpegts_api_getServiceType(pmt_info->sdt_info->service_type));			
			sprintf(program_info->service_name,"%s",pmt_info->sdt_info->service_name);
			sprintf(program_info->service_provider_name,"%s",pmt_info->sdt_info->service_provider_name);
		}else{
			ms_u08 sdt_index=0;
			for(sdt_index=0;sdt_index<rlmpegts->sdtInfo.sdt_num;sdt_index++){
				SDTInfo *sdt_info=&rlmpegts->sdtInfo.sdt_info[sdt_index];	
				if(program_info->program_num==sdt_info->serviceid){
					program_info->flag_hasSdt=ms_true;
					pmt_info->sdt_info=&rlmpegts->sdtInfo.sdt_info[sdt_index];
					ms_strcpy(program_info->service_type,mpegts_api_getServiceType(sdt_info->service_type));			
					sprintf(program_info->service_name,"%s",sdt_info->service_name);
					sprintf(program_info->service_provider_name,"%s",sdt_info->service_provider_name);
					break;
				}
			}
		}
	}
	return 1;
}
static ms_s32 mpegts_rebuild(void *param,ms_byte * buf)
{
	ms_bufcheckRet(-1, param);
	MPEGTSContxt *rlmpegts=(MPEGTSContxt *)param;
	
/*必须找到所有信息，且节目数量大于0*/	
	if(ms_false==rlmpegts->flag_findAllInfo
		||0==rlmpegts->paInfo.totalnum_program){
		return 0;
	}	
	ms_u32 len=0;

/*重新编辑PAT|PMT表：发送间隔200毫秒*/	
	if((rlmpegts->stream_info->flag_rebuild_pat||rlmpegts->stream_info->flag_rebuild_pmt)
		&&mstime_api_counterAsyncUs_Up(&rlmpegts->Etimep_pat_pmt, ms_usmseconds(200))
	){
		if(rlmpegts->pat_ctt.totalnum!=rlmpegts->paInfo.totalnum_program){
			rlmpegts->pat_ctt.totalnum=rlmpegts->paInfo.totalnum_program;
			for(ms_u16 index=0;index<rlmpegts->paInfo.totalnum_program;index++){
				PMTInfo *pmt_info=&rlmpegts->pmtInfo[index];
				MAPContxt *pmap_ctt=mpegts_innerapi_findMap(rlmpegts, pmt_info->program_num,ms_false);
			/*获取节目号*/
				rlmpegts->pmt_ctt[index].program_num=rlmpegts->pat_ctt.serviceidList[index]= \
					(pmap_ctt&&pmap_ctt->flag_map_program_num) ? pmap_ctt->program_num : pmt_info->program_num;
			/*获取PMT的PID*/
				rlmpegts->pmt_ctt[index].pid=rlmpegts->pat_ctt.pmtIdList[index]= \
					(pmap_ctt&&pmap_ctt->flag_map_pmt_pid) ? pmap_ctt->pmt_pid : pmt_info->pid;
			/*获取PCR的PID*/
				rlmpegts->pmt_ctt[index].pcr_pid=(pmap_ctt&&pmap_ctt->flag_map_pcr_pid) ? pmap_ctt->pcr_pid : pmt_info->tableInfo.pid_pcr;
			/*获取ES数量*/
				rlmpegts->pmt_ctt[index].num_es=pmt_info->tableInfo.num_es;
			/*获取ES信息：PID和streamType*/
				for(ms_u08 iEs=0;iEs<pmt_info->tableInfo.num_es;iEs++){
					if(pmap_ctt){
						for(ms_u08 iMEs=0;iMEs<pmt_info->tableInfo.num_es;iMEs++){
							if(pmt_info->tableInfo.esInfo[iEs].pid==pmap_ctt->es_pid_org[iMEs]){
								rlmpegts->pmt_ctt[index].EsInfo[iEs].pid=pmap_ctt->es_pid[iMEs];
								break;
							}
						}
						if(!rlmpegts->pmt_ctt[index].EsInfo[iEs].pid){
							rlmpegts->pmt_ctt[index].EsInfo[iEs].pid=pmt_info->tableInfo.esInfo[iEs].pid;
						}
					}else{
						rlmpegts->pmt_ctt[index].EsInfo[iEs].pid=pmt_info->tableInfo.esInfo[iEs].pid;
					}
					rlmpegts->pmt_ctt[index].EsInfo[iEs].streamType=pmt_info->tableInfo.esInfo[iEs].streamType;
				}
			}
		}
	/*重新编辑PAT*/		
		if(rlmpegts->stream_info->flag_rebuild_pat){
			ms_u32 len_patInfo=mpegts_api_buildPat(&buf[len],&rlmpegts->pat_cc,rlmpegts->pktSize, 0x01, 
				rlmpegts->paInfo.transport_stream_id,&rlmpegts->pat_ctt);
			len+=len_patInfo;
		}
	/*重新编辑PMT表*/		
		if(rlmpegts->stream_info->flag_rebuild_pmt){
			for(ms_u08 index=0;index<rlmpegts->paInfo.totalnum_program;index++){
				ms_u32 len_pmtInfo=mpegts_innerapi_buildPmt2(&buf[len], &rlmpegts->pmt_ctt[index].cc, rlmpegts->pktSize, 
					0x01, &rlmpegts->pmt_ctt[index]);
				len+=len_pmtInfo;
			}	
		}
	}

/*重新编辑SDT表：前提为需要重新生成、已找齐SDT信息，且间隔500毫秒*/	
	if(rlmpegts->stream_info->flag_rebuild_sdt
		&&rlmpegts->sdtInfo.sdt_num==rlmpegts->paInfo.totalnum_program
		&&mstime_api_counterAsyncUs_Up(&rlmpegts->Etimep_sdt, ms_usmseconds(500))
	){
		if(rlmpegts->sdt_ctt.totalnum!=rlmpegts->sdtInfo.sdt_num){
			rlmpegts->sdt_ctt.totalnum=rlmpegts->sdtInfo.sdt_num;
			for(ms_u16 index=0;index<rlmpegts->sdtInfo.sdt_num;index++){
				PMTInfo *pmt_info=&rlmpegts->pmtInfo[index];
				MAPContxt *pmap_ctt=mpegts_innerapi_findMap(rlmpegts, pmt_info->program_num,ms_false);
				if(pmt_info->sdt_info){
mssdtInfo:	
				/*获取节目号*/
					rlmpegts->sdt_ctt.serviceidList[index]= \
						(pmap_ctt&&pmap_ctt->flag_map_program_num) ? pmap_ctt->program_num : pmt_info->program_num;
					rlmpegts->sdt_ctt.service_type[index]=pmt_info->sdt_info->service_type;
				/*获取运营商名称*/
					if(pmap_ctt&&ms_buflen(pmap_ctt->service_provider_name)){
						ms_strcpy(rlmpegts->sdt_ctt.spnList[index], pmap_ctt->service_provider_name);
					}else{
						ms_strcpy(rlmpegts->sdt_ctt.spnList[index], pmt_info->sdt_info->service_provider_name);
					}
				/*获取节目名称*/	
					if(pmap_ctt&&ms_buflen(pmap_ctt->service_name)){
						ms_strcpy(rlmpegts->sdt_ctt.snList[index], pmap_ctt->service_name);
					}else{
						ms_strcpy(rlmpegts->sdt_ctt.snList[index], pmt_info->sdt_info->service_name);
					}
				}else{
					for(ms_u16 sdt_index=0;sdt_index<rlmpegts->sdtInfo.sdt_num;sdt_index++){
						SDTInfo *sdt_info=&rlmpegts->sdtInfo.sdt_info[sdt_index];	
						if(pmt_info->program_num==sdt_info->serviceid){
							pmt_info->sdt_info=&rlmpegts->sdtInfo.sdt_info[sdt_index];
							goto mssdtInfo;
						}
					}
				}
			}
		}
	/*重新编辑SDT表*/	
		ms_u32 len_sdtInfo=sdt_api_build(&buf[len],&rlmpegts->sdt_cc,rlmpegts->pktSize, 0x03,
			rlmpegts->sdtInfo.transport_streamid,rlmpegts->sdtInfo.original_network_id,&rlmpegts->sdt_ctt);
		len+=len_sdtInfo;
	}
	return len;
}
static ms_void mpegts_deinit(ms_void **param)
{
	ms_bufcheck(param);
	ms_deMalloc((*param));
}

AVParser ffurl_mpegts_parser={
	.name			=AVFORMAT_MPEGTS,
	.longname		="Use to parse mpegts info",
	.init			=mpegts_init,
	.parse			=mpegts_parse,
	.update			=mpegts_update,
	.rebuild		=mpegts_rebuild,	
	.deinit			=mpegts_deinit,	
};
#undef MPEGTS_C
#endif
